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
- 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 string
In this case, we can use Python "any()" .
Here the script return "Found a match", because at least one word exists in the list.
How to check if string contains substring from list
If 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 Keywords
You 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 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:
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.