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

640次阅读  |  发布于6年以前

本文实例讲述了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程序设计有所帮助。

Copyright© 2013-2020

All Rights Reserved 京ICP备2023019179号-8