check if * in string python code example

Example 1: python test if number in string

>>> def hasNumbers(inputString):
...     return any(char.isdigit() for char in inputString)
... 
>>> hasNumbers("I own 1 dog")
True
>>> hasNumbers("I own no dog")
False

Example 2: python string contains substring

fullstring = "StackAbuse"
substring = "tack"

if fullstring.find(substring) != -1:
    print "Found!"
else:
    print "Not found!"

Example 3: python check if string is in input

try:
   val = int(userInput)
except ValueError:
   print("That's not an int!")

Example 4: python str contains word

fullstring = "StackAbuse"
substring = "tack"

if substring in fullstring:
    print "Found!"
else:
    print "Not found!"

Example 5: check if string contains python

>>> str = "Messi is the best soccer player"
>>> "soccer" in str
True
>>> "football" in str
False

Tags:

C Example