xxxxxxxxxx
txt = "foobar"
print("foo" in txt)
xxxxxxxxxx
fullstring = "StackAbuse"
substring = "tack"
if substring in fullstring:
print("Found!")
else:
print("Not found!")
xxxxxxxxxx
# Basic syntax:
any(elem in your_string for elem in elements)
# Where:
# - elements is an iterable
# - any() will return true if any of the elements in elements is in
# your_string, otherwise it returns False
# Example usage:
your_string = 'a short example string'
any(elem in your_string for elem in ['Malarkey', 23, 'amp'])
--> True # any() returns True because amp is in your_string
xxxxxxxxxx
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
xxxxxxxxxx
string = 'ex@mple'
if '@' in string:
return True
if '@' not in string:
return False
xxxxxxxxxx
def is_value_in_string(value: str, the_string: str):
return value in the_string.lower()
xxxxxxxxxx
import timeit
def func1():
phrase = 'Lucky Dog'
return any(i in 'LD' for i in phrase)
def func2():
phrase = 'Lucky Dog'
if ('L' in phrase) or ('D' in phrase):
return True
else:
return False
if __name__ == '__main__':
func1_time = timeit.timeit(func1, number=100000)
func2_time = timeit.timeit(func2, number=100000)
print('Func1 Time: {0}\nFunc2 Time: {1}'.format(func1_time, func2_time))