Check whether a Python string contains another string


To test whether a given python string contains another string, you can use python's 'in' operator as shown below:

mystring = "The white fox jumped over the brown fence."
if "fence" in mystring:
print("it's in the string")
it's in the string
mystring = "The white fox jumped over the brown fence."
if "blue" not in mystring:
print("it's not in the string")
it's not in the string



Ace your next data science interview

Get better at data science interviews by solving a few questions per week



Find a bug? Submit a suggested change on Github, or message me on Twitter.