How do i check if a string contains multiple substrings in python?
If you want exact matches of words then consider word tokenizing the target string. I use the recommended word_tokenize from nltk: Show
Here is the tokenized string from the accepted answer:
The accepted answer gets modified as follows:
As in the accepted answer, the word "more" is still matched. If "mo" becomes a match string, however, the accepted answer still finds a match. That is a behavior I did not want.
Using word tokenization, "mo" is no longer matched:
That is the additional behavior that I wanted. This answer also responds to the duplicate question here. Summary: In this tutorial, we will learn different ways to check for multiple substrings in another string in Python. Return True if the string contains any of the given substrings. In this method, we iterate through the list of substrings and check using the We append the boolean
results to a list and pass it to The
Output: True One drawback of this method is that it is case-sensitive. If a substring is present in the main string, but the case does not match, it will return false result. We can overcome this by changing the strings to the same case (e.g lower), inside of the loop body:
Alternatively, we can check for substring using the regular expression as discussed below. Method 2: Using any() with regular expression (re)Using regular expressions, we can easily check multiple substrings in a single-line statement. We use the
Output: True This is one of the fastest method to check whether a string contains any of the given multiple substrings in Python. I am an engineer by education and writer by passion. I started this blog to share my little programming wisdom with other programmers out there. Hope it helps you. Python any() FunctionPython any() function accepts iterable (list, tuple, dictionary etc.) as an argument and return true if any of the element in iterable is true , else it returns false . If the iterable object is empty, the any() function will return False. any Vs all
Check if multiple strings exist in another stringIn this case, we can use Python "any()" . Here the script return "Found a match", because at least one word exists in the list. example 2:outputHow to check if string contains substring from listIf your list is too long, it is better to use Python Regular Expression . Above example return "Found a match" because "one" is exist in the list. Check If a String Contains Multiple KeywordsYou can also find a solution for this by using iteration . Above script return "Found a match" because "one" is exist in the myList. All matches including duplicates in a stringIf you want to get all the matches including duplicates from the list: First word match in a string from listIf you want the first match with False as a default: Above example return "one" because the word "one" is the starting word and exists in the myList also. How to extract the first and final words from a string?Similarly to check if all the strings from the list are found, use "all" instead of "any" . Above example return False because "six" is not in the string. How do you check if a string contains a list of substrings Python?The easiest way to check if a Python string contains a substring is to use the in operator. The in operator is used to check data structures for membership in Python. It returns a Boolean (either True or False ).
How do I check if a string contains more than one character in Python?Using Python's "in" operator
The simplest and fastest way to check whether a string contains a substring or not in Python is the "in" operator . This operator returns true if the string contains the characters, otherwise, it returns false .
How do you check if a list contains multiple values in Python?Use the all() function to check if multiple values are in a list, e.g. if all(value in my_list for value in multiple_values): . The all() function will return True if all of the specified values are in the list and False otherwise.
How do you check for multiple strings in a string?You can use any : a_string = "A string is more than its parts!" matches = ["more", "wholesome", "milk"] if any(x in a_string for x in matches): Similarly to check if all the strings from the list are found, use all instead of any .
|