参考资料:菜鸟教程
find()函数
find() 函数检测字符串中是否包含子字符串 str ,如果指定 beg(开始) 和 end(结束) 范围,则检查是否包含在指定范围内,如果包含子字符串返回开始的索引值,否则返回-1。
str1.find(str2, beg = 0, end = len(str1))
参数含义:
- str1:被检测字符串;
- str2:字串;
- beg:下标起点,默认为0;
- end:下标终点,默认为被检测字符串的长度。
例:
str1 = "this is an example"
str2 = "example"
print(str1.find(str2))
print(str1.find(str2, 0, 18))
print(str1.find(str2, 12, 18))
输出为:
11
11
-1