小编典典

Python是否具有字符串“包含”子字符串方法?

python

我在寻找Python中的string.containsor string.indexof方法。

我想要做:

if not somestring.contains("blah"):
   continue

阅读 467

收藏
2020-02-08

共2个答案

小编典典

你可以使用in运算符

if "blah" not in somestring: 
    continue
2020-02-08
小编典典

如果只是子字符串搜索,则可以使用string.find("substring")

你必须与小心一点find,index和in虽然,因为它们是字符串搜索。换句话说,这是:

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

它将打印Found 'is' in the string.类似,if "is" in s:结果为True。这可能是你想要的,也可能不是。

2020-02-08