check if string contains char python code example
Example 1: python string contains substring
fullstring = "StackAbuse"
substring = "tack"
if fullstring.find(substring) != -1:
print "Found!"
else:
print "Not found!"
Example 2: python string contains
>>> str = "Messi is the best soccer player"
>>> "soccer" in str
True
>>> "football" in str
False
Example 3: python check if character is letter
>>> 'A'.isalpha()
True
>>> '1'.isalpha()
False
Example 4: how to check for a substring in python
def find_string(string,sub_string):
return string.find(sub_string)
#.find() also accounts for multiple occurence of the substring in the given string
Example 5: if substring not in string python
>>> string = "Hello World"
>>> # Check Sub-String in String
>>> "World" in string
True
>>> # Check Sub-String not in String
>>> "World" not in string
False
Example 6: python if string contains char
myString = "<text contains this>"
myOtherString = "AnotherString"
# Casting to string is not needed but it's good practice
# to check for errors
if str(myString) in str(myOtherString):
# Do Something
else:
# myOtherString didn't contain myString