Do you want to check if a list of substrings is present in the string? This article will show you how to check if a string contains a list of Substring.
You have a list of substrings present and all you want to check if all of the substrings are present in the given string or not. In this case, you can loop through the list of a substring in Python, and then you can easily check in one line whether the list of substrings is present or not.
1. Using For Loop Through Substring and If Any condition
You can loop through all the substrings in the list of substrings and then check using the if any condition to check if the given substring is present in the string.
Note: if any condition will check if any of substring is present in the list and it will return True.
Let us see the usage of For Loop and in condition for the above problem in actual Code.
#Initializing the String givenString = "Hi You are Visiting Coduber.com Website." #Creating a List of Substring I need to check subStringList = ['Hi', 'Coduber.com', 'are', 'What'] #Checking if the above Substrings are present in the #givenString or not if any(word in givenString for word in subStringList): print("Some or All the Substring Found in the Given String.") else: print("None of the Substring Found in Given String.")
Output:
Some or All the Substring Found in the Given String.
2. Using Loop and If All Condition
Now suppose you want to check if all the words or substrings present in the List if available in the given string. For that case, you need to use all conditions of Python and then use the loop as used in the above code.
Let us see the in the below code the usage of if all condition to check if the list of substring is contained in the string.
#Initializing the String givenString = "Hi You are Visiting Coduber.com Website." #Creating a List of Substring I need to check subStringList = ['Hi', 'Coduber.com', 'are', 'What'] #Checking if the above Substrings are present in the #givenString or not if all(word in givenString for word in subStringList): print("Some or All the Substring Found in the Given String.") else: print("Not all of the Substring Found in Given String.")
Output:
Not all of the Substring Found in Given String.
3. Using Regex to Check if List of substring is present in String
This problem can also be resolved using the regex library of Python. In the regex library, the method compile can be used to join all the substrings and then verify if the compiled substring is present in the given string or not.
Let us see in the below example code the usage of compile method of regex library in Python.
#importing Regex import re #Initializing the String givenString = "Hi You are Visiting Coduber.com Website." #Creating a List of Substring I need to check subStringList = ['Hi', 'Coduber.com', 'are', 'What'] #Checking if the above Substrings are present in the #givenString or not if re.compile('|'.join(subStringList), re.IGNORECASE).search(givenString): print("Substring Found in the Given String.") else: print("Substrings not found in given String.")
Output:
Substring Found in the Given String.
So, the important thing to note here is that if any of the substrings is present in the given string then this condition will return True.
Wrap Up
I hope you were able to get knowledge about how to check if the list of substrings or words is in another string or given string.
Let me know in the comment section if you know the most efficient method than the one listed above I will be happy to add that method here.
If you liked the above tutorial then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.
Further Read: