Python find()方法

FIND函数方法

str.find(str, beg=0, end=len(string))

参数解释
str – 指定检索的字符串
beg – 开始索引,默认为0。
end – 结束索引,默认为字符串的长度。
如果包含子字符串返回开始的索引值,否则返回-1

例子1

str1 = "this is string example....wow!!!"
 
print str1.find( "exam")
print str1.find( "exam", 10)  #10 代表从index为10开始寻找 
print str1.find( "exam", 40)  #40 代表从index为40开始寻找 
output:
15
15
-1

例子2

str1 = 'abca'
print str1.find('a')    
# 从下标0开始,查找在字符串里第一个出现的子串,返回结果:0
print str1.find('a',1)  
# 从下标1开始,查找在字符串里第一个出现的子串:返回结果3
print info.find('3')    
# 查找不到返回-1

参考文章:
https://www.runoob.com/python/att-string-find.html

你可能感兴趣的:(日常编程ERROR小错误学习)