python判断字符串是否包含子字符串

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

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

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

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

1 a = 'http://www.baidu.com/'
2 if "baidu" in a:
3    print('a contains abc')
View Code

二、使用find函数实现contains的功能

1 s = "This be a string"
2 if s.find("is") == -1:
3   print "No 'is' here!"
4 else:
5   print "Found 'is' in the string."
View Code

 

以上2种方法都可以!

 

posted @ 2017-09-13 21:37  彩笔杀手  阅读(16332)  评论(0)    收藏  举报