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:
var_list = ['forum' , 'website']
Now, call the “print()” function to view the input value of string and list:
print("My initialized list is : " + str(var_list))
Use the “for” loop and check the condition with “if” statement from the input string:
Now, view the checked condition use a “bool()” method inside the “print()” statement:
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:
Call the “print()” function to display the filtered result:
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:
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.