Python Tutorial

Python Flow Control

Python Functions

Python Data Types

Python Date and Time

Python Files

Python String

Python List

Python Dictionary

Python Variable

Python Input/Output

Python Exceptions

Python Advanced

Check if multiple strings exist in another string, and find the matches in Python

To check if multiple strings exist in another string and find the matches, you can use a list comprehension with the in keyword, or use the re module to find all occurrences of the substrings. The re module provides a more powerful way to search for matches, as it allows you to use regular expressions.

Here's an example using a list comprehension and the in keyword:

text = "This is a sample text with apple and banana in it."
substrings = ["apple", "banana", "orange"]

matches = [substring for substring in substrings if substring in text]

print("Matches:", matches)
# Output: Matches: ['apple', 'banana']

In this example, we use a list comprehension to iterate over the substrings list and check if each substring is present in the text string using the in keyword. The resulting matches list contains the substrings that were found in the text.

Here's an example using the re module:

import re

text = "This is a sample text with apple and banana in it."
substrings = ["apple", "banana", "orange"]

# Create a regular expression pattern with the substrings
pattern = re.compile("|".join(map(re.escape, substrings)))

matches = pattern.findall(text)

print("Matches:", matches)
# Output: Matches: ['apple', 'banana']

In this example, we first import the re module. We then create a regular expression pattern by joining the substrings with the | (or) operator, which allows the pattern to match any of the substrings. We use the re.escape() function to escape any special characters in the substrings. Then, we use the findall() method of the compiled pattern to find all matches in the text string. The resulting matches list contains the substrings that were found in the text.

  1. Python check if multiple substrings exist in a string:

    def check_substrings(string, substrings):
        return all(sub in string for sub in substrings)
    
    # Example usage
    string = "Hello, this is a sample string."
    substrings = ["Hello", "sample", "Python"]
    result = check_substrings(string, substrings)
    print(result)  # False
    
  2. Check if any of multiple strings exist in a larger string:

    def any_substring_exists(string, substrings):
        return any(sub in string for sub in substrings)
    
    # Example usage
    string = "This is a Python example."
    substrings = ["Java", "Python", "C++"]
    result = any_substring_exists(string, substrings)
    print(result)  # True
    
  3. Finding common substrings in a string in Python:

    def common_substrings(string, substrings):
        return [sub for sub in substrings if sub in string]
    
    # Example usage
    string = "This is a Python example."
    substrings = ["Java", "Python", "C++"]
    result = common_substrings(string, substrings)
    print(result)  # ['Python']
    
  4. Check if a string contains any of a list of words in Python:

    def contains_any_words(string, words):
        return any(word in string for word in words)
    
    # Example usage
    string = "This is a Python example."
    words = ["Java", "Python", "C++"]
    result = contains_any_words(string, words)
    print(result)  # True
    
  5. Python regex for matching multiple strings in a text:

    import re
    
    def regex_match(string, patterns):
        return any(re.search(pattern, string) for pattern in patterns)
    
    # Example usage
    string = "This is a Python example."
    patterns = ["Java", "Python", "C++"]
    result = regex_match(string, patterns)
    print(result)  # True
    
  6. Search and count occurrences of multiple strings in Python:

    def count_occurrences(string, substrings):
        return {sub: string.count(sub) for sub in substrings}
    
    # Example usage
    string = "Python is great. Python is powerful."
    substrings = ["Python", "is"]
    result = count_occurrences(string, substrings)
    print(result)  # {'Python': 2, 'is': 2}
    
  7. Python set intersection for finding common words in a string:

    def common_words(string1, string2):
        words1 = set(string1.split())
        words2 = set(string2.split())
        common = words1.intersection(words2)
        return list(common)
    
    # Example usage
    string1 = "Python is powerful."
    string2 = "Python is versatile."
    result = common_words(string1, string2)
    print(result)  # ['Python', 'is']
    
  8. Check if all elements of a list are in a string in Python:

    def all_elements_in_string(elements, string):
        return all(element in string for element in elements)
    
    # Example usage
    elements = ["Python", "is", "powerful"]
    string = "Python is powerful and versatile."
    result = all_elements_in_string(elements, string)
    print(result)  # True