Python是否具有字符串“包含”子字符串方法?
我在寻找Python中的string.containsor string.indexof
方法。
我想要做:
if not somestring.contains("blah"):
continue
-
你可以使用in运算符:
if "blah" not in somestring: continue
-
如果只是子字符串搜索,则可以使用
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
。这可能是你想要的,也可能不是。