Multiple strings exist in another string : Python

In Python, to check if multiple strings exist in another string, you can use various approaches like the "in" operator, regular expressions, or custom functions. Let's explore these methods with examples:

Using "in" Operator

The "in" operator allows you to check if individual substrings exist within a given string.

text = "The quick brown fox jumps over the lazy dog." substrings = ["quick", "fox", "lazy"] for substring in substrings: if substring in text: print(f"Substring '{substring}' found.") else: print(f"Substring '{substring}' not found.")

Using re.findall()

For more complex pattern matching involving multiple substrings, you can use the re.findall() function from the re module.

import re text = "The quick brown fox jumps over the lazy dog." substrings = ["quick", "fox", "lazy"] for substring in substrings: if re.findall(substring, text): print(f"Substring '{substring}' found.") else: print(f"Substring '{substring}' not found.")

Python any() Function

The any() function takes an iterable (such as a list, tuple, dictionary, etc.) as an argument and evaluates to true if at least one element within the iterable is true; otherwise, it returns false. If the iterable object is empty, the any() function will return False. The any() function proves particularly useful when you need to check for the presence of truthy values within an iterable, enabling efficient and concise evaluation of multiple elements.

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.

Conclusion

Each of these methods allows you to check for the existence of multiple substrings within a given string. The choice of method depends on the complexity of the patterns you need to match and your specific use case. The "in" operator is simple and straightforward for basic checks, while regular expressions offer greater flexibility for advanced pattern matching involving more complex substrings. Custom functions provide modularity and code reusability for repetitive substring checks. Choose the most suitable method based on your requirements to efficiently determine if multiple strings exist in another string in Python.