How to Check if String Contains Substring in Python?

There are many situations where we need to find whether a string contains a substring in Python. There are many ways to check that. In this article, we will learn how to check if any substring exists within a Python string.
Methods to Check if String Contains Substring in Python
Python offers several methods to check if any substring exists within a Python string efficiently, such as find(), index(), count(), and others. We'll discuss the following approaches:
- Using If-Else
- Using In Operator
- Checking using split() method
- Using find() method
- Using "count()" method
- Using index() metho:
- Using list comprehension
- Using lambda function
- Using contains” magic class
- Using Slicing Function
- Using Regular Expressions
- Using operator contains() method
Check Substring in String using the if-else in python
In this method, we will check how to :
- Python contains a substring in a string, and we can use an if-else statement to determine whether a Python substring exists within a string.
- The if-else statement enables us to check multiple conditions and then it will run different blocks of code depending on whether the condition is true or not.
- Using this method, we can check whether a Python contains a substring or not.
Time Complexity: Its time complexity is O(n), where n is the length of the main string. This method iterates over each character in the string to perform the comparison.
Check if Python String contains substring Using "in" Operator
- The in operator is a membership operator in Python that checks for the presence of a value within a sequence.
- Using this method, we can check whether a python contains a substring or not.
Time Complexity: The time complexity is O(n), where n is the length of the main string. The in operator iterates over each character in the string to perform the search.
Check if string contains substring Using String.count() Method in python
- The main_string variable contains the string "Python is a powerful programming language. Python is used for various purposes."
- The substring variable contains the string "Python".
- The main_string.count(substring) statement counts the how many times the Python contains within the main string.
- If the count is greater than zero, the substring is present, and the corresponding message is printed.
- If the count is zero, it indicates that the substring is not present, and the appropriate message is printed.
Time Complexity: The time complexity is O (n). This method iterates over the characters in the string to count the occurrences of the substring.
Check if string contains substring Using String.index() Method in python
Time Complexity: The time complexity is O(n), where n is the length of the main string. This method iterates over the characters in the string to find the index of the substring.
Check if Python String contains substring Using String.find() method
The method uses the find() method in Python to find the index of the first occurrence of a substring within a string.
- If the substring is found in the main string, then the method returns the index of its first occurrence.
- If not found, it returns -1.
Time Complexity: The time complexity is O(n), where n is the length of the main string. This method iterates over the characters in the string to find the index of the substring.
Check if string contains substring using Regular Expression in Python (Regex Method)
The re.search() function performs a regex search for the substring within the main string (main_string).
- If there is any substring is found within the main string, the re.search() function returns a match object, which returns to True
- Then, code block inside the if statement is executed, printing the message "Substring found using Regular Expression (Regex)".
Time Complexity: The time complexity is O(n). This method iterates over the characters in the string to perform the regex search.
Check if the string contains a substring in python using the split() method
- The code explains the main_string into individual words using the split() method.
- It checks if the substring exists among these words.
- If the substring is found in the list of words, it prints "Substring found using split() method."
- Otherwise, it prints "Substring not found".
Time Complexity: The time complexity is O(n + m), n = length of the main string, and m is the number of words in the split string. This method splits the string and then iterates over the words to perform the search.
Check if string contains substring in python Using list comprehension
- In this example, the variable substring contains Python as a substring and will check whether it is present in main_string using the split method.
- If the substring is found in any word, it prints "Substring found using list comprehension"; otherwise, it prints "Substring not found".
Time Complexity: This iterates over words in the split string, which takes O(n) time. Then, Checking if the substring is in each word adds an additional O(m) time complexity, where m is the length of the substring. So, the overall time complexity of this method is O(n * m).
Check if string contains substring in python Using lambda function
We can use a lambda function along with the any() function to check if a substring is present in a Python string.
In this example, the variable substring contains Python as a substring, and we need to check this with the lambda function.
Time Complexity: Iterates over words in the split string, taking O(n) time, where n is the length of the main string. Then, Checking if the substring is in each word adds an additional O(m) time complexity, where m is the length of the substring. So, the overall time complexity of this method is O(n * m).
Check if string contains substring in python Using "contains” magic class
The contains in python is a magic method for checking whether a substring exists within a string. Using this method, we can get a boolean value which tells us whether the String contains the specified substring.
Here's an example to demonstrate contains in python:
Explanation
- When used with the __contains__() method, the in operator checks if the substring is present within the main string.
- The condition returns true if the substring is found within the main string. Otherwise, it will return false.
Time Complexity: It utilizes the contains() magic method of strings, and it's time complexity is O(n).
Check if string contains substring in python Using Slicing Function
- The for loop iterates over each index i in the range from 0 to the length of the main string minus the length of the substring plus one.
- This ensures that you iterate only over the indices where the substring could potentially start.
- Within the loop, the substring starting from index i and extending to i + len(substring) is extracted using slicing: main_string[i+len(substring)].
Time Complexity: In this, iterating over indices from 0 to len(main_string) - len(substring) + 1, taking O(n - m) time. Slicing main_string[i+len(substring)] adds O(m) time complexity. Thus, in the worst case, the overall time complexity is O(n * m).
Check if string contains substring in python using operator.contains() method
In the provided code, the operator.contains() function checks if the substring s2 is present within the string s.
Here's another example to demonstrate the usage of operator.contains():
In this example:
-
The op.contains() function checks if the substring s2 present within the string s .
-
Since the substring "learning" is present in the string, this condition becomes True, and the output will be:
-
This example demonstrates how to use the operator.contains() to check for substrings in Python strings.
-
Using this method, we can check whether a Python contains a substring or not.
Time Complexity: It checks if the substring is present within the string, and its time complexity is O(n).
Conclusion
- There are various methods in python for checking if a python contains a substring or not like if-else statements, in operator, count() method, index() method, find() method, regular expressions, and more.
- Each method has its time complexity, ranging from O(n) to O(n * m).
- Methods like if-else statements and the in operator offer simplicity and readability with an O(n) time complexity.