Check if multiple Strings exist in another String in Python

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
5 min

banner

# Table of Contents

  1. Check if ONE of multiple Strings exists in another String
  2. Check if ALL of multiple Strings exist in another String

# Check if one of multiple strings exists in another string

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.

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

check if one of multiple strings exists in another string

The code for this article is available on GitHub

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.

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.

# Getting the substring that is contained in the 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')
The code for this article is available on GitHub
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.

# Getting one or more substrings that are contained in the string

Use the filter() method if you need to get one or more substrings that are contained in the string.

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

getting one or more substrings that are contained in the string

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.

# Check if One of multiple Strings exists in another String using a for loop

You can also use a for loop to check if one of multiple strings exists in another string.

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

check if one of multiple strings exists in another string using for loop

The code for this article is available on GitHub

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.

# Checking in a case-insensitive manner

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')
The code for this article is available on GitHub

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.

# Check if ALL of multiple strings exist in another string

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.

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.

# Check if ALL of multiple strings exist in another string using a for loop

You can also use a for loop to check if all of multiple substrings exist in a string.

main.py
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')
The code for this article is available on GitHub

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.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev