Python String contains: Check if String Contains Substring

Python String Contains Thumbnail

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:

  1. Using the find() Method
  2. Using the in Operator
  3. Using the index() Method
  4. Using the regular expression
  5. 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.

Syntax:

string.__contains__(substring)

Parameters:

Here, substring is the string you want to search, and string is the string object in which you want to search.

Return:

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.

Example 1

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.

Output:

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.

Example 2

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")

Output:

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__ :

  1. Code Readability: Well, it is clear that where other method looks readable, the __contains__ method looks complicated.
  2. Limited Functionality: This method can only check a single string, it is not able to perform complex string operations.
  3. 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.
  4. 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.

Conclusion

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.

Reference

https://stackoverflow.com/questions/3437059/does-python-have-a-string-contains-substring-method