s = "kitti"
0,1,2,3,4
k,i,t,t,i
how do i retrieve '4', the last index value. i know i can do a len(s)-1, but i assume there is a function that gives me last number of index, or more eloquent way. Maybe not, just checking
thanks
>>> s = 'kitti'
>>> idx = len(s) -1
>>> s[idx]
'i'
>>> s[-1]
'i'
>>> s[len(s)-1]
'i'
>>> len(s)-1
4
>>>
I can think of couple of solutions
def last_index(string_, char):
index = -1
for pos, c in enumerate(string_):
if c == char:
index = pos
return index
def last_index_short(string_, char):
return length(string_) - string_[::-1].index(char) - 1
>>> s = 'kitty'
>>> s.rindex(s[-1])
4