找不到下面代码的子字符串错误

时间:2015-06-02 09:59:06

标签: python string

s2 = "fast and furious series from one to seven"
print "s2.index('e', 50, 0) : ", s2.index('e', 50, 0)

错误:

print "s2.index('e', 50, 0) : ", s2.index('e', 50, 0)
ValueError: substring not found

1 个答案:

答案 0 :(得分:0)

你要求Python在空切片中找到e;第二个和第三个参数是 start end 索引。 s2[50:0]是一个空字符串:

>>> s2 = "fast and furious series from one to seven"
>>> s2[50:0]
''

如果您想通过搜索结尾查找文字,请使用str.rindex() function

>>> s2.rindex('e')
39