Python--判断一个字符串是否包含某子串的几种方法
发布日期:2021-07-01 04:21:09 浏览次数:2 分类:技术文章

本文共 580 字,大约阅读时间需要 1 分钟。

(1).find()方法:

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

看示例:

print(result3)string = 'this is string'>>>result1 = string.find('is')>>>print(result1)2>>>result2 = string.find('me')>>>print(result2)-1>>>result3 = string.find(' ')>>>print(result3)4

函数原型:

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

参数描述:

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

返回值:

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

(2)in方法:

看示例:

>>>string = 'this is string'>>>result = 'is' in string>>>print(result)true>>>result = 'is' not in string>>>print(result)False

转载地址:https://mtyjkh.blog.csdn.net/article/details/79775497 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:pandas包
下一篇:Python--切片学习记录

发表评论

最新留言

路过按个爪印,很不错,赞一个!
[***.219.124.196]2024年04月10日 03时40分27秒