Multiple strings exist in another string : Python
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
- any will return True when at least one of the elements is Truthy.
- all will return True only when all the elements are Truthy.
Check if multiple strings exist in another stringIn this case, we can use Python "any()" .
myList = ['one', 'six','ten'] str = "one two three four five" if any(x in str for x in myList): print ("Found a match") else: print ("Not a match")
Here the script return "Found a match", because at least one word exists in the list.
myList = ['one', 'six','ten'] str = "one two three four five" isMatch = [True for x in myList if x in str] if True in isMatch: print("some of the strings found in str") else: print("no strings found in str")output
some of the strings found in str
How to check if string contains substring from listIf your list is too long, it is better to use Python Regular Expression .
import re myList = ['six','ten','One'] str = "one two three four five" if any(re.findall(''.join(myList), str, re.IGNORECASE)): print("Found a match") else: print("Not Found a match")
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 .
myList = ['six','ten','one'] str = "one two three four five" match = False for item in myList: if item in str: match = True if match: print("Found a match") else: print("No match found")
Above script return "Found a match" because "one" is exist in the myList.
All matches including duplicates in a string
If you want to get all the matches including duplicates from the list:
First word match in a string from list
If you want the first match with False as a default:
myList = ['one', 'six','ten'] str = "one two three four five" firstWord = next((x for x in myList if x in str), "False") print(firstWord)
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.
- Keywords in Python
- Python Operator - Types of Operators in Python
- Python Variables and Data Types
- Python Shallow and deep copy operations
- Python Datatype conversion
- Python Mathematical Function
- Basic String Operations in Python
- Python Substring examples
- How to check if Python string contains another string
- Memory Management in Python
- Python Identity Operators
- What is a None value in Python?
- How to Install a Package in Python using PIP
- How to update/upgrade a package using pip?
- How to Uninstall a Package in Python using PIP
- How to call a system command from Python
- How to use f-string in Python
- Python Decorators (With Simple Examples)
- Python Timestamp Examples