[python] Test a string for a substring

Is there an easy way to test a Python string "xxxxABCDyyyy" to see if "ABCD" is contained within it?

This question is related to python string

The answer is


if "ABCD" in "xxxxABCDyyyy":
    # whatever

There are several other ways, besides using the in operator (easiest):

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