Does Python have a string 'contains' substring method?
If it's just a substring search you can use string.find("substring")
.
You do have to be a little careful with find
, index
, and in
though, as they are substring searches. In other words, this:
s = "This be a string"
if s.find("is") == -1:
print("No 'is' here!")
else:
print("Found 'is' in the string.")
It would print Found 'is' in the string.
Similarly, if "is" in s:
would evaluate to True
. This may or may not be what you want.
Use the in
operator:
if "blah" not in somestring:
continue