用python语言,如何判断一段字符串中是否包含指定的字符串

投稿:夜雨微澜 优质问答领域创作者 发布时间:2023-10-14 07:53:12
用python语言,如何判断一段字符串中是否包含指定的字符串

python的string对象没有contains方法,不用使用string.contains的方法判断是否包含子字符串,但是python有更简单的方法来替换contains函数。方法1:使用 in 方法实现contains的功能:site = ''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."