测试字符串是否包含子字符串
有没有简单的方法可以检查一个Python字符串“xxxxABCDyyyy”里面是否包含“ABCD”?
2 个回答
36
除了使用 in
操作符(这是最简单的方法)之外,还有几种其他的方法:
index()
>>> try:
... "xxxxABCDyyyy".index("test")
... except ValueError:
... print "not found"
... else:
... print "found"
...
not found
find()
>>> if "xxxxABCDyyyy".find("ABCD") != -1:
... print "found"
...
found
re
>>> import re
>>> if re.search("ABCD" , "xxxxABCDyyyy"):
... print "found"
...
found
225
if "ABCD" in "xxxxABCDyyyy":
# whatever
当然可以!请把你想要翻译的内容发给我,我会帮你用简单易懂的语言解释清楚。