>>> word = 'Python' >>> word[0] # character in position 0 'P' >>> word[5] # character in position 5 'n' >>> word[-1] # last character 'n' >>> word[-2] # second-last character 'o' >>> word[-6] 'P' >>> word[0:2] # characters from position 0 (included) to 2 (excluded) 'Py' >>> word[2:5] # characters from position 2 (included) to 5 (excluded) 'tho' >>> word[:2] + word[2:] 'Python' >>> word[:4] + word[4:] 'Python' >>> word[:2] # character from the beginning to position 2 (excluded) 'Py' >>> word[4:] # characters from position 4 (included) to the end 'on' >>> word[-2:] # characters from the second-last (included) to the end 'on' >>> word[::-1] # reverse a string 'nohtyp' >>> len(word) # length of a string 6
+---+---+---+---+---+---+ | P | y | t | h | o | n | +---+---+---+---+---+---+ 0123456 -6 -5 -4 -3 -2 -1