Python

Python Check If String Contains Substring From List

In Python, the strings are the combination of characters contained in quotes. Like strings, lists are also used to save a collection of strings. While working with Python, users frequently encounter situations where they must determine if the provided string contains a substring from any given Python list. To resolve this encountered scenario, multiple methods are used.

This guide will provide different techniques to check if a string contains a substring from the list in Python.

How to Check If String Contains Substring From List in Python?

To check if the string has a substring from the list in Python, the following techniques are used:

Method 1: Check If String Contains Substring From List in Python through List Comprehension

To check if the list contains the substring from the Python list, the list comprehension can be used. The implementation of the list comprehension is stated in the below example.

Example

First, create a string variable and pass a string. Then, define and initialize a list:

first_str = "LinuxHint is the world best tutorial website"

var_list = ['forum' , 'website']

Now, call the “print()” function to view the input value of string and list:

print("My input string is : " + first_str)

print("My initialized list is : " + str(var_list))

Use the “for” loop and check the condition with “if” statement from the input string:

resultant_str = [x for x in var_list if(x in first_str)]

Now, view the checked condition use a “bool()” method inside the “print()” statement:

print("Is my string contain a list element? " + str(bool(resultant_str)))

According to the below given output, the provided string contains the substring from the initialized Python list:

Method 2: Check If String Contains Substring From List in Python Using “any()” Method

Another way to check if the string contains a substring from the Python list or not is the “any()” method. This method checks each list element to an input string match.

Example

Call the “any()” method along with the “for” loop to check the list variable and store it into the “resultant_str” variable:

resultant_str = any(x in first_str for x in var_list)

Call the “print()” function to display the filtered result:

print("Is my string contain a list element? " + str(resultant_str))

Output

Method 3: Check If String Contains Substring From List in Python Using “for” Loop

We can also use the “for” loop for checking if the provided string contains a substring from the list in Python or not. The “for” loop is the iterative function that iterates over the provided sequence of the string element.

Example

Use the “for” loop to check the substring by utilizing the “if” condition, and print the desired result through the print statement:

for substring in var_list:

if substring in first_str:

print("String contain a list element")

break

It can be observed the provided string contains the substring from the previously initialized Python list:

That’s all! You have learned about the different techniques to view if a string has a substring from the list in Python.

Conclusion

To check if the provided string has the substring from the provided list in Python, the “list comprehension”, the “any()” method, and the iterative function “for” loop are used. All techniques first check the provided condition and then display the results. This guide illustrated multiple ways to check if a string contains a substring from the list in Python.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.