Indices may be negative numbers, to start counting from the right. For example: : String Index « String « Python






Indices may be negative numbers, to start counting from the right. For example:

 

word = 'Help' + 'A'


print word[-1]     # The last character
print word[-2]     # The last-but-one character
print word[-2:]    # The last two characters
print word[:-2]    # All but the last two characters

   
  








Related examples in the same category

1.String indexing: Random AccessString indexing: Random Access
2.An index that is too large is replaced by the string size
3.String indexing: from front or endString indexing: from front or end