描述:
Python find() 方法检测字符串中是否包含子字符串 str ,如果指定 beg(开始) 和 end(结束) 范围,则检查是否包含在指定范围内,如果包含子字符串返回开始的索引值,否则返回-1。
语法:
str.find(str, beg=0, end=len(string))
参数:
str – 指定检索的字符串
beg – 开始索引,默认为0。
end – 结束索引,默认为字符串的长度。
返回值:
如果包含子字符串返回开始的索引值,否则返回-1。
实例:
>>> #指定查找字符串,有结果
>>> print('This is a test.'.find('s'))
3
>>> #指定查找字符串,无结果
>>> print('This is a test.'.find('ok'))
-1
>>> #指定查找开始位置
>>> print('This is a test.'.find('s',4))
6
>>>#指定查找开始及终止位置
>>> print('This is a test.'.find('s',7,20))
12