我想要找到给定输入字符串str
中某个子字符串最后出现的位置(或索引)。
例如,假设输入字符串是str = 'hello'
,子字符串是target = 'l'
,那么它应该输出3。
我该怎么做呢?
发布于 2012-03-06 03:15:13
发布于 2012-03-06 03:15:19
使用str.rindex
方法。
>>> 'hello'.rindex('l')
3
>>> 'hello'.index('l')
2
发布于 2014-10-13 22:10:43
试试这个:
s = 'hello plombier pantin'
print (s.find('p'))
6
print (s.index('p'))
6
print (s.rindex('p'))
15
print (s.rfind('p'))
https://stackoverflow.com/questions/9572490
复制相似问题