登录
首页精彩阅读python判断字符串是否包含子字符串的方法
python判断字符串是否包含子字符串的方法
2017-08-14
收藏

python判断字符串是否包含子字符串的方法

本文实例讲述了python判断字符串是否包含子字符串的方法。分享给大家供大家参考。具体如下:

python的string对象没有contains方法,不用使用string.contains的方法判断是否包含子字符串,但是python有更简单的方法来替换contains函数。
方法1:使用 in 方法实现contains的功能:    
site = 'http://www.jb51.net/'
if "jb51" in site:
   print('site contains jb51')

输出结果:site contains jb51

方法2:使用find函数实现contains的功能    
s = "This be a string"
if s.find("is") == -1:
  print "No 'is' here!"
else:
  print "Found 'is' in the string."

希望本文所述对大家的Python程序设计有所帮助。

数据分析咨询请扫描二维码

客服在线
立即咨询