Last updated: Apr 9, 2024
Reading timeยท5 min
Use the any()
function to check if multiple strings exist in another
string.
The any()
function will return True
if at least one of the multiple
strings exists in the string.
my_str = 'apple, egg, avocado' list_of_strings = ['apple', 'banana', 'kiwi'] if any(substring in my_str for substring in list_of_strings): # ๐๏ธ this runs print('At least one of the multiple strings exists in the string') else: print('None of the multiple strings exist in the string')
If you need to check if ALL of multiple strings exist in another string, click on the following subheading.
We used a generator expression to iterate over the collection of strings.
In the example, we iterate over the collection of strings and check if each string is contained in the other string.
The any function
takes an iterable as an argument and returns True
if any element in the
iterable is truthy.
The any()
function will short-circuit returning True
if at least one string
is contained in the other string.
You can use the assignment expression syntax if you need to get the value that is contained in the string.
my_str = 'apple, egg, avocado' list_of_strings = ['apple', 'banana', 'kiwi'] if any((match := substring) in my_str for substring in list_of_strings): # ๐๏ธ this runs print('At least one of the multiple strings exists in the string') print(match) # ๐๏ธ 'apple' else: print('None of the multiple strings exist in the string')
NAME := expression
syntax.If the iterable we pass to the any()
function is empty or none of the elements
in the iterable are truthy, the any
function returns False
.
Use the filter()
method if you need to get one or more substrings that are
contained in the string.
my_str = 'apple, egg, kiwi' list_of_strings = ['one', 'two', 'apple', 'egg'] matches = list(filter(lambda x: x in my_str, list_of_strings)) print(matches) # ๐๏ธ ['apple', 'egg']
The filter() function takes a function and an iterable as arguments and constructs an iterator from the elements of the iterable for which the function returns a truthy value.
The lambda function we passed to filter
gets called with each substring from
the list.
The new list only contains the substrings that are contained in the string.
for
loopYou can also use a for loop to check if one of multiple strings exists in another string.
my_str = 'apple, egg, avocado' list_of_strings = ['apple', 'banana', 'kiwi'] one_exists = False for substring in list_of_strings: if substring in my_str: one_exists = True break print(one_exists) # ๐๏ธ True if one_exists: # ๐๏ธ this runs print('At least one of the substrings is contained in the string') else: print('None of the substrings are contained in the string')
We initialized the one_exists
variable to False
and used a for
loop to
iterate over the list of strings.
On each iteration, we check if the current substring is contained in the string.
If the condition is met, we set the one_exists
variable to True
and exit the
for
loop.
If you need to check if one of multiple strings exists in another string in a case-insensitive manner, convert both strings to lowercase.
my_str = 'APPLE, EGG, AVOCADO' list_of_strings = ['apple', 'banana', 'kiwi'] if any(substring.lower() in my_str.lower() for substring in list_of_strings): # ๐๏ธ this runs print('At least one of the multiple strings exists in the string') # print(match) # ๐๏ธ 'apple' else: print('None of the multiple strings exist in the string')
We used the str.lower()
method to convert each substring and the string to
lowercase before checking if each substring is contained in the string.
The str.lower method returns a copy of the string with all the cased characters converted to lowercase.
Use the all()
function to check if multiple strings exist in another
string.
The all()
function will return True
if all of the substrings exist in the
string and False
otherwise.
my_str = 'apple, egg, kiwi' list_of_strings = ['apple', 'egg', 'kiwi'] # โ check if ALL of multiple strings exist in another string if all(substring in my_str for substring in list_of_strings): # ๐๏ธ this runs print('All of the substrings exist in the string') else: print('Not all of the substrings exist in the string')
We used a generator expression to iterate over the collection of strings.
The all() built-in function
takes an iterable as an argument and returns True
if all elements in the
iterable are truthy (or the iterable is empty).
If any of the strings in the collection is not contained in the other string,
the all()
function will short-circuit returning False
.
for
loopYou can also use a for
loop to check if all of multiple substrings exist in a
string.
my_str = 'apple, egg, kiwi' list_of_strings = ['apple', 'egg', 'kiwi'] all_exist = True for substring in list_of_strings: if not substring in my_str: all_exist = False break print(all_exist) # ๐๏ธ True if all_exist: # ๐๏ธ this runs print('All of the substrings exist in the string') else: print('Not all of the substrings exist in the string')
We initialized the all_exist
variable to True
and used a for
loop to
iterate over the list of strings.
On each iteration, we check if the current substring is not contained in the string.
If the condition is met, we set the all_exist
variable to False
and exit the
for
loop.
The break statement breaks out of the
innermost enclosing for
or while
loop.
I've also written an article on how to check if multiple keys exist in a dictionary.
You can learn more about the related topics by checking out the following tutorials: