>>> w = "abcde"
>>> w[0]
'a'
>>> w[4]
'e'
If you want to specify the rightmost character, use [-1].
>>> w = "abcde"
>>> w[-5]
'a'
>>> w[-1]
'e'
>>> w = "abcde"
>>> w[5]
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
word[5]
IndexError: string index out of range
>>> w[-6]
Traceback (most recent call last):
File "<pyshell#18>", line 1, in <module>
w[-6]
IndexError: string index out of range
>>> w = "abcde"
>>> w[1:4]
'bcd'
>>> w[0:3]
'abc'
>>> w[:3]
'abc'
>>> w[3:0]
''
>>> w[-1:]
'e'
>>> w[:-1]
'abcd'
>>> word[-4:-2]
'bc'
>>> word[-2:-4]
''
Recommended Posts