Check if multiple strings exist in another string in Python

avatar

Borislav Hadzhiev

Last updated: Aug 10, 2022

banner

Photo from Unsplash

Check if multiple strings exist in another string in Python #

Use the any() function to check if multiple strings exist in another string, e.g. if any(substring in my_str for substring in list_of_strings):. The any() function will return True if at least one of the multiple strings exists in the string.

main.py
my_str = 'apple, egg, avocado' list_of_strings = ['apple', 'banana', 'kiwi'] # ✅ check if ONE of multiple strings exists in another string 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') # ------------------------------------------------------------ # ✅ check if ALL of multiple strings exist in another string if all(substring in my_str for substring in list_of_strings): print('All of the substrings exist in the string') else: # 👇️ this runs print('Not all of the substrings exist in the string')

We used a generator expression to iterate over the collection of strings.

Generator expressions are used to perform some operation for every element or select a subset of elements that meet a condition.

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.

main.py
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')
Assignment expressions allow us to assign to variables within an expression using the 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.

If you need to check if one of multiple strings exists in another string in a case-insensitive manner, convert both strings to lowercase.

main.py
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, e.g. if all(substring in my_str for substring in list_of_strings):. The all() function will return True if all of the substrings exist in the string and False otherwise.

main.py
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.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.