美文网首页
python二级--有人还不知道find的用法

python二级--有人还不知道find的用法

作者: young十三 | 来源:发表于2019-07-30 18:47 被阅读0次

一、描述

\color{red}{Python find()} 方法检测字符串中是否包含子字符串 str ,如果指定 beg(开始) 和 end(结束) 范围,则检查是否包含在指定范围内,如果包含子字符串返回开始的索引值,否则返回-1。

二、语法

1、find()方法语法:

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

2、参数

  • str -- 指定检索的字符串
  • beg -- 开始索引,默认为0
  • end -- 结束索引,默认为字符串的长度

3、返回值

如果包含子字符串返回开始的索引值,否则返回-1。

三、实例

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

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

输出结果:

15
26
-1

相关文章

网友评论

      本文标题:python二级--有人还不知道find的用法

      本文链接:https://www.haomeiwen.com/subject/ancorctx.html