Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to check if multiple strings exist in another string in Python?
While working with strings, checking if a string exists inside another string is a common task and can be achieved using the in keyword. In this article, we'll explore different methods to check if multiple strings exist in another string in Python.
For example, checking if multiple strings like "TP" and "WELCOME" exist in the string "WELCOME to TP". Python provides several approaches including any(), all(), and regular expressions.
Using any() Function
The any() function returns True if at least one element in an iterable is truthy. We can use it with a generator expression to check if any of our target strings exist in the main string ?
Syntax
any(iterable)
Example
Here's how to check if any of multiple strings exist in a target string ?
target_strings = ['Python', 'Java', 'C++']
main_string = "I love Python programming"
print("Main string:", main_string)
print("Looking for:", target_strings)
if any(substring in main_string for substring in target_strings):
print("At least one string found!")
else:
print("No strings found")
Main string: I love Python programming Looking for: ['Python', 'Java', 'C++'] At least one string found!
Using all() Function
The all() function returns True only if all elements in an iterable are truthy. Use this when you need to verify that every target string exists in the main string ?
Syntax
all(iterable)
Example
This example checks if all specified strings exist in the target string ?
main_string = "Welcome to TutorialsPoint Python tutorial"
required_strings = ["Welcome", "Python", "tutorial"]
print("Main string:", main_string)
print("Required strings:", required_strings)
if all(substring in main_string for substring in required_strings):
print("All strings found!")
else:
print("Some strings are missing")
Main string: Welcome to TutorialsPoint Python tutorial Required strings: ['Welcome', 'Python', 'tutorial'] All strings found!
Using Regular Expressions
The re module provides powerful pattern matching capabilities. We can use re.findall() with the pipe operator | to create a pattern that matches any of our target strings ?
Example
Here's how to use regex to find multiple strings ?
import re
main_string = "Python is great for data science and web development"
search_strings = ['Python', 'data', 'Java']
print("Main string:", main_string)
print("Searching for:", search_strings)
# Create pattern: 'Python|data|Java'
pattern = '|'.join(search_strings)
matches = re.findall(pattern, main_string)
if matches:
print(f"Found: {matches}")
else:
print("No matches found")
Main string: Python is great for data science and web development Searching for: ['Python', 'data', 'Java'] Found: ['Python', 'data']
Comparison of Methods
| Method | Returns | Best For |
|---|---|---|
any() |
True if any string found | Check if at least one exists |
all() |
True if all strings found | Verify all strings exist |
re.findall() |
List of actual matches | Get which strings were found |
Conclusion
Use any() to check if at least one string exists, all() to verify all strings exist, and re.findall() when you need to know exactly which strings were found. Choose the method based on your specific requirements.
