Sunday, March 21, 2021

Python Pool: Best Ways to Use Python String __contains__() Method

Introduction

There are many ways to check if the substring is present on the main string in python in python. In that, one of the methods is Python String __contains__(). This method is used to check if the string is present in the other string or not. So, In this topic, we will be discussing string__contains__().

What is Python String __contains__??

Python string __contains__() 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, and if the string is not present in the other string, then the value returned is False. In python, the characters are case sensitive.

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 in the form of two strings. In which we will apply string __contains__() and check whether the given string is present in the other string or not.

#input string

str = "Latracalsolutions"

str1 = "sol"

value = str.__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 and can be called the substring, which 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 return the value in boolean.

2. Taking users input

In this example, we will take the input from the user as a string. In which we will apply python string __contains__() and check whether the given string is present in the other string or not.

#users input
str = input("Enter the main string : ")

str1 = input("Enter the substring : ")

value = str.__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, and that can be called the substring, which 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 return the value in boolean.

3. string not present in given input

In this example, the input will be given in the form of two strings. In which we will apply python string __contains__() and check whether the given string is present in the other string or not.

#input string

str = "Latracalsolutions"

str1 = "sols"

value = str.__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, which 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 return 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 = str.__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 which you think is suitable for your program.

The post Best Ways to Use Python String __contains__() Method appeared first on Python Pool.



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...