python if string in string 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: if substring not in string python

fullstring = "StackAbuse"
substring = "tack"

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

Example 3: python string contains

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

Example 4: python check if string in string

if "blah" not in somestring: 
    continue

Example 5: python check if string

type('hello world') == str
# output: True

type(10) == str
# output: False

Example 6: how to check a string in if statement python

if var is 'stringone' or 'stringtwo':
    dosomething()

Tags:

Php Example