Python字符串find()方法

Python字符串find()方法确定字符串str是出现在字符串中,还是在字符串指定范围的子串中,子字符串是由给给定起始索引beg和结束索引end切片得出。

语法

以下是find()方法的语法 -

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

参数

  • str - 指定要搜索的字符串。
  • beg - 这是起始索引,默认为0
  • end - 这是结束索引,默认情况下它等于字符串的长度。

返回值

  • 如果找到则返回其索引,否则返回-1

示例

以下示例显示了find()方法的用法。

#!/usr/bin/python3

str1 = "this is string example....wow!!!"
str2 = "exam";

print (str1.find(str2))
print (str1.find(str2, 10))
print (str1.find(str2, 40))

当运行上面的程序,它产生以下结果 -

15
15
-1

上一篇: Python字符串 下一篇: Python列表