
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to check if multiple strings exist in another string in Python?
In this article, we are going to see how to check if multiple strings exist in another string in Python.
Imagine a scenario where we must satisfy 50 requirements in order to work. To determine whether conditions are true, we typically employ conditional statements (if, if−else, elif, nested if). However, with so many conditions, the code grows long and unreadable due to the excessive use of if statements.
Python's any() function is the only option for such circumstances, thus programmers must use it.
The first approach is by writing a function to check for any multiple strings’ existence and then using the ‘any’ function for checking if any case is True, if any case is True then, True is returned, otherwise False is returned
Example 1
In the example given below, we are taking a string as input and we are checking if there are any matches with the strings of the match using the any() function −
match = ['a','b','c','d','e'] str = "Welcome to Tutorialspoint" print("The given string is") print(str) print("Checking if the string contains the given strings") print(match) if any(c in str for c in match): print("True") else: print("False")
Output
The output of the above example is as shown below −
The given string is Welcome to Tutorialspoint Checking if the string contains the given strings ['a', 'b', 'c', 'd', 'e'] 15. True
Example 2
In the example given below, we are taking program same as above but we are taking a different string and checking −
match = ['a','b','c','d','e'] str = "zxvnmfg" print("The given string is") print(str) print("Checking if the string contains the given strings") print(match) if any(c in str for c in match): print("True") else: print("False")
Output
The output of the above example is given below −
The given string is zxvnmfg Checking if the string contains the given strings ['a', 'b', 'c', 'd', 'e'] False
Using Reggular Expressions
Regular expressions are used in the second method. Import the re library and install it if it isn't already installed to use it. We'll use Regex and the re.findall() function to see if any of the strings are present in another string after loading the re library.
Example 1
In the example given below, we are taking a string as input and multiple strings and are checking if any of the multiple strings match with the string using Regular Expressions −
import re match = ['a','b','c','d','e'] str = "Welcome to Tutorialspoint" print("The given string is") print(str) print("Checking if the string contains the given strings") print(match) if any(re.findall('|'.join(match), str)): print("True") else: print("False")
Output
The output of the above example is as shown below −
The given string is Welcome to Tutorialspoint Checking if the string contains the given strings ['a', 'b', 'c', 'd', 'e'] True
Example 2
In the example given below, we are taking program same as above but we are taking a different string and checking −
import re match = ['a','b','c','d','e'] str = "zxvnmfg" print("The given string is") print(str) print("Checking if the string contains the given strings") print(match) if any(re.findall('|'.join(match), str)): print("True") else: print("False")
Output
The output of the above example is given below −
The given string is zxvnmfg Checking if the string contains the given strings ['a', 'b', 'c', 'd', 'e'] False
- Related Articles
- Check if given multiple keys exist in a dictionary in Python
- How can we check if specific string occurs multiple times in another string in Java?
- MySQL query to check if multiple rows exist?
- How to check if a substring is contained in another string in Python
- Check if a string can be repeated to make another string in Python
- Check if a string is suffix of another in Python
- Check if string contains another string in Swift
- Check if it is possible to transform one string to another in Python
- Check if binary string multiple of 3 using DFA in Python
- How to check if a string is a subset of another string in R?
- Check If a String Can Break Another String in C++
- Check if given string can be split into four distinct strings in Python
- How to check if a String contains another String in a case insensitive manner in Java?
- Check if a string can be formed from another string using given constraints in Python
- Check if a string can be obtained by rotating another string 2 places in Python
