解释
rfind() 返回字符串最后一次出现的索引,如果没有匹配项则返回-1
find() 返回字符串第一次出现的索引,如果没有匹配项则返回-1
两者句法相同
句法
str_super.rfind(substr, start,end)
substr:想要查找的字符串
start:开始查找的索引,并且包含此索引
end:结束查找的索引,并且不包含此索引
str_super:想要查找substr的字符串
用法
1)substr只含一个字符
str_super = "this is really a string example....wow!!!"
substr = "s"
print(str_super.rfind(substr))
print(str_super.rfind(substr, 0, 10))
print(str_super.rfind(substr, 10, 0))
print(str_super.find(substr))
print(str_super.find(substr, 0, 10))
print(str_super.find(substr, 10, 0))
结果:
17
6
-1
3
3
-1
2)substr含多个字符
str = “this is really a string example…wow!!!”
substr = “is”
print(str_super.rfind(substr))
print(str_super.rfind(substr, 0, 10))
print(str_super.rfind(substr, 10, 0))
print(str_super.find(substr))
print(str_super.find(substr, 0, 10))
print(str_super.find(substr, 10, 0))
结果:
5
5
-1
2
2
-1