Check if Multiple Strings Exist in Another String in Python



While working with the strings, Checking if the string exists inside another string is a common task and can be achieved by using the in keyword. But, In this article we are going to see how to check if multiple strings exist in another string in Python.

For example, checking the multiple strings: "TP", "WELCOME", exists in the string "WELCOME to TP". For achieving this, Python provides different methods including loops, any() and all().

Using Python any() Function

The Python any() function returns true if any of the element of the given iterable (such as list, tuple, set or dictionary) is truthy, otherwise it returns false.

In this case, we are using the any() function with the generator expression, where each element in the list is checked using the in operator to see if it is present in the target string.

Syntax

Following is the syntax for Python any() function -

any(iterable)

Example

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")

The output of the above program is as follows -

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

Using Python all() Function

The Python all() function returns true if all the elements of the given iterable are truthy, otherwise it returns false.

In this scenario, we are using the generator expression with all() and using the main string we are checking the each item in the list, ensuring the result is true if the substring is found in the string.

Syntax

Following is the syntax for Python all() function -

all(iterable)

Example

Let's look at the following example, where we are going to consider multi-line coded string as input and we are finding out the output of that using the exec() method.

x = "Welcome To The TutorialsPoint"
y = ["To", "TutorialsPoint"]
z = all(sub in x for sub in y)
print("Result :", z)

The following is the output of the above program -

Result : True

Using Python Re Module

The python re module supports for the regular expressions, helping in string patterns matching and manipulation. It includes functions like match(), search() to identify, extract or replace patterns in strings.

In this case, 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

Consider the following example, where we are going to use the re module and checking if any of the multiple strings match with the target string.

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")

The output of the above program is as follows -

The given string is
Welcome to Tutorialspoint
Checking if the string contains the given strings
['a', 'b', 'c', 'd', 'e']
True
Updated on: 2025-04-21T15:57:16+05:30

10K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements