Python Check In String
Check In String
To check if a certain phrase or character is present in a string, we can use the keywords
in
or not in
.
Example
Check if the phrase "ain" is present in the following text:
txt = "The rain in Spain stays mainly in the plain"
x = "ain" in txt
print(x)
Try it Yourself »
x = "ain" in txt
print(x)
Example
Check if the phrase "ain" is NOT present in the following text:
txt = "The rain in Spain stays mainly in the plain"
x = "ain" not in txt
print(x)
Try it Yourself »
x = "ain" not in txt
print(x)
Related Pages
Python Strings Tutorial String Literals Assigning a String to a Variable Multiline Strings Strings are Arrays Slicing a String Negative Indexing on a String String Length Format String Escape Characters