Multiple strings exist in another string : Python

Python any() Function

Python 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

  1. any will return True when at least one of the elements is Truthy.
  2. all will return True only when all the elements are Truthy.

Check if multiple strings exist in another string

In 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.

example 2:

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 list

If 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 Keywords

You 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:


string contains one of several substrings Python

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?


extract the first and final words from a string in python
Similarly to check if all the strings from the list are found, use "all" instead of "any" .
Check list of words in another string python

Above example return False because "six" is not in the string.