How to check if multiple strings exist in another string in Python?


In this article, we are going to see how to check if multiple strings exist in another string in Python.

Imagine a scenario where we must satisfy 50 requirements in order to work. To determine whether conditions are true, we typically employ conditional statements (if, if−else, elif, nested if). However, with so many conditions, the code grows long and unreadable due to the excessive use of if statements.

Python's any() function is the only option for such circumstances, thus programmers must use it.

The first approach is by writing a function to check for any multiple strings’ existence and then using the ‘any’ function for checking if any case is True, if any case is True then, True is returned, otherwise False is returned

Example 1

In the example given below, we are taking a string as input and we are checking if there are any matches with the strings of the match using the any() function 

match = ['a','b','c','d','e']
str = "Welcome to Tutorialspoint"

print("The given string is")
print(str)

print("Checking if the string contains the given strings")
print(match)

if any(c in str for c in match):
   print("True")
else:
   print("False")

Output

The output of the above example is as shown below −

The given string is
Welcome to Tutorialspoint
Checking if the string contains the given strings
['a', 'b', 'c', 'd', 'e'] 15. 
True

Example 2

In the example given below, we are taking program same as above but we are taking a different string and checking 

match = ['a','b','c','d','e']
str = "zxvnmfg"

print("The given string is")
print(str)

print("Checking if the string contains the given strings")
print(match)

if any(c in str for c in match):
   print("True")
else:
   print("False") 

Output

The output of the above example is given below −

The given string is
zxvnmfg
Checking if the string contains the given strings
['a', 'b', 'c', 'd', 'e']
False

Using Reggular Expressions

Regular expressions are used in the second method. Import the re library and install it if it isn't already installed to use it. We'll use Regex and the re.findall() function to see if any of the strings are present in another string after loading the re library.

Example 1

In the example given below, we are taking a string as input and multiple strings and are checking if any of the multiple strings match with the string using Regular Expressions 

import re
match = ['a','b','c','d','e']
str = "Welcome to Tutorialspoint"

print("The given string is")
print(str)

print("Checking if the string contains the given strings")
print(match)

if any(re.findall('|'.join(match), str)):
   print("True")
else:
   print("False")

Output

The output of the above example is as shown below −

The given string is
Welcome to Tutorialspoint
Checking if the string contains the given strings
['a', 'b', 'c', 'd', 'e']
True

Example 2

In the example given below, we are taking program same as above but we are taking a different string and checking −

import re
match = ['a','b','c','d','e']
str = "zxvnmfg"

print("The given string is")
print(str)

print("Checking if the string contains the given strings")
print(match)

if any(re.findall('|'.join(match), str)):
   print("True")
else:
   print("False")

Output

The output of the above example is given below 

The given string is
zxvnmfg
Checking if the string contains the given strings
['a', 'b', 'c', 'd', 'e']
False

Updated on: 07-Dec-2022

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements