好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

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

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

python的string对象没有contains方法,不用使用string.contains的方法判断是否包含子字符串,但是python有更简单的方法来替换contains函数。

方法1:使用 in 方法实现contains的功能:

site = 'http://HdhCmsTest'
if "jb51" in site:
   print('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程序设计有所帮助。

查看更多关于python判断字符串是否包含子字符串的方法的详细内容...

  阅读:36次