Introduction
There are many ways to check if the substring is present on the main string in Python. In that, one of the methods is Python String __contains__(). This method is used to check whether the string is present in the other string. So, In this topic, we will discuss string__contains__().
What is Python String __contains__??
Python string __contains__(), which is the instance method used to return the boolean value, i.e., True or False. If the string is present in the other string, then the value returned is True; if the string is not present in the other string, then the value returned is False. In Python, the characters are case-sensitive.
__ contains __ method is used to overload Python’s “in” operator. For example, you have a custom class MyClass and want to check if one instance is in another by using it.
class myClass():
def __init__(self, name):
self.name=name
def __contains__(self, substr):
if substr in self.name:
return True
else:
return False
ob = myClass("latracal")
print ('latra' in ob)
print ('Latra' in ob)
Output:
True
False
In such case you’ll use the contains a method to overload this “in” operator.
Moreover, the __ methods__ are not expected to be used in real life. For example, you don’t use
4.__add__(3)
you’ll use 4+3
Syntax
value = string.__contains__(substr)
Return value
The return value is the boolean value i.e. True or False.
Examples to Use Python string __contains__
Let us understand the Python string__contains__() method in details with the help of examples:
1. Given Input
In this example, the input will be given as two strings. We will apply string __contains__() and check whether the given string is present in the other string.
#input string
s = "Latracalsolutions"
str1 = "sol"
value = s.__contains__(str1)
print("Output : ",value)
Output:
Output : True
Explanation:
Here, firstly, we have taken the given string as str. Secondly, we have taken another string with str1, which can be called the substring, and we need to check that it is present in the above string.
Thirdly, we have applied the string __contains__() function and stored the value variable’s output. At last, we have printed the output. The output will be in boolean as the function returns the value in boolean.
2. Taking user’s input
In this example, we will take the input from the user as a string. In which we will apply the python string __contains__() and check whether the given string is present in the other string or not.
#users input
s = input("Enter the main string : ")
str1 = input("Enter the substring : ")
value = s.__contains__(str1)
print("Output : ",value)
Output:
Enter the main string : Latracalsolutions
Enter the substring : solutions
Output : True
Explanation:
Here, firstly, we have taken the input from the user in the string as str. Secondly, we have taken another input string with str1 from the user, which can be called the substring, which we need to check is present in the above string. Thirdly, we have applied the string __contains__() function and stored the value variable’s output. At last, we have printed the output. The output will be in boolean as the function returns the value in boolean.
3. String not present in a given input
In this example, the input will be given as two strings. In which we will apply the python string __contains__() and check whether the given string is present in the other string or not.
#input string
s = "Latracalsolutions"
str1 = "sols"
value = s.__contains__(str1)
print("Output : ",value)
Output:
Output : False
Explanation:
Here, firstly, we have taken the given string as str. Secondly, we have taken another string with str1 and can be called the substring, and we need to check that it is present in the above string. Thirdly, we have applied the string __contains__() function and stored the value variable’s output. At last, we have printed the output. The output will be in boolean as the function returns the value in boolean.
4. Using string __contains__() as a class method
In this example, we will apply the function python string __contains__() and write the input string inside it only and see the output.
#input string inside the function
value = s.__contains__('Latracals', 'Lat')
print("Output : ", value)
Output:
Output : True
Explanation:
In this example, we have used the function as the class method, which means the input string is passed inside the function only, and it will be a one-line code to check whether the string is present inside the string or not. So, we have applied the string __contains__(). Inside it, we have passed both the string and stored the output in the value variable. At last, we have printed the output. The output will be in boolean as the function return the value in boolean.
Conclusion
In this tutorial, we have discussed all the different ways to use the string __contains__() methods in detail, with examples of each way explained in detail. Through example, you can understand the concept more clearly. You can use any way that you think is suitable for your program.
Everything about this article is just wrong. You are not supposed to use the __contains__ method directly, it is just implementation for the “in” operator. And the same goes for any dunder (double underscore) methods, they are not meant to be used directly.
So the correct way to check if string is contained in another string is
Please change the title or at least put a big warning at the top, so that some python beginner doesn’t think that using __contains__ is actually the best and correct way to do it.
PS. Also don’t use “str” as a variable name as it conflicts with the built-in str and can cause unexpected errors.
Hi,
We’ve added a section of using __contains__ for “in” operator. Please let me know if you have any other doubts.
Regards,
Pratik