In Python, many times when you’re working with substrings you want to find whether a substring is inside another string or not, there can be many reasons for that like searching and filtering data, input validation, etc. For this, Python provides us with a simple and efficient method to accomplish this task.
This article will show you the various built-in functions to check if a string contains a substring in Python. The main function that is our main focus for this article is Python’s __contains__() method, so let’s get started.
Methods to check if a Python String Contains a Substring
Python offers many ways to check whether a String contains other substrings or not. Some of them are given below:
- Using the find() Method
- Using the in Operator
- Using the index() Method
- Using the regular expression
- Using the string __contains__()
The ‘in’ operator, find(), and index() methods are frequently used to check for a substring within a string by developers and we have attached the link to our tutorial on each above-mentioned string method so that you can read them too, but for this tutorial, we will be using the built-in function __contains__().
Python String contains
The Python string __contains__() function can be used with a string object, it takes a substring as an argument and returns True if the substring is present in the string.
Here, substring is the string you want to search, and string is the string object in which you want to search.
The return type of this function is a boolean value, it is either True or False, depending on whether the substring is found on the specified string.
Example of using String __contains__() Method
Let’s look at some examples of how to check whether a string contains a substring in Python.
str = "Hello World!" sub_str = "World" if (str.__contains__(sub_str)): print("String contains Substring") else: print("String does not contain Substring")
Here we have specified a string, and then a substring, both containing some character, and then we have used the if statement and used the string.__contains__() method which returns True if the substring we specified is present in the string, and if so the statement inside the if statement will execute otherwise the statement inside the else statement will execute.
String contains Substring
Since “World” is a substring of the original string, “Hello World!”, the return value is True and the if statement is executed.
Let’s try to check for the substring “world”.
str = "Hello World!" sub_str = "world" if (str.__contains__(sub_str)): print("String contains Substring") else: print("String does not contain Substring")
String does not contain Substring
This method is case sensitive, so the string “world” is considered different from the string “World”, hence it returns False, and the statement inside the else block is executed.
Avoid using String __contains__() Method
There is no programming logic that prohibits the use of the __contains__() method, but there are some reasons why it is not recommended to use it.
Below are some reasons why you should avoid using __contains__ :
- Code Readability: Well, it is clear that where other method looks readable, the __contains__ method looks complicated.
- Limited Functionality: This method can only check a single string, it is not able to perform complex string operations.
- Not suitable for complex data structures: The __contains__ method is not able to work properly with complex data structures having multiple levels of nesting or objects with complex relationships.
- Alternatives: Another reason is that there are many alternatives, which we have mentioned at the beginning of this tutorial, which are more reliable and similar to this method, so there is no point in using it.
In this article, we learned to use string.__contains__() method to find whether a substring is present inside a string or not, if the substring is found it returns True, if the substring is not found it returns False. It is not the best way to check whether a substring appears in the string but it is the fastest. Hope you enjoyed reading this tutorial.