Signup/Sign In

Python Program to Check if a Substring is Present in a Given String

In this tutorial, we will learn to check if a substring is present in a given string or not. Strings in Python are data structures for storing characters in a sequence. A substring can be defined as a sequence of characters within another string.

In our program, for a given string and substring we will check if the substring is a part of the string or not and return the result accordingly. For example,

Input:

string="Python Language"

substring="thon"

Output: Yes

To solve this problem there are several different approaches, some of them are:

  1. Using in operator
  2. Using find()
  3. Using contains()

We will discuss all these approaches separately below.

Approach 1: using in operator

In this approach, we will use the in operator to check for a substring. This is the fastest method to check if a substring is present or not. The in operator will check if a value exists in the given sequence and return True if it exists, if the value does not exist in the string then it returns False.

Algorithm

Follow the algorithm to understand the approach better.

Step 1- Define a string with values

Step 2- Define substring which will be checked

Step 3- Check if the substring is in string

Step 4- Print yes if it exists

Step 5- Else print no

Python Program 1

Look at the program to understand the implementation of the above-mentioned approach. We have used the in operator with the if conditional statement to check if the substring is a part of the string.

string="StudyTonight"
substring="Study"

print("Does ",substring," exist in ",string,"?")
if substring in string:
    print("Yes")
else:
    print("False")


Does Study exist in StudyTonight ?
Yes

Approach 2: using find() function

The find() is a built-in string function in the python library that returns the lowest index at which the substring occurs in the string. It will return -1 if the substring is not found. We will use this string method and check the value returned by this function to get the final result.

Algorithm

Follow the algorithm to understand the approach better.

Step 1- Define a string with values

Step 2- Define substring which will be checked

Step 3- Declare a variable to store the value returned by the function

Step 4- Check if the value of the variable is greater than or equal to 0

Step 5- Print yes if the condition is satisfied

Step 6- Else print no

Python Program 2

Look at the program to understand the implementation of the above-mentioned approach. If the find() method returns 0 or a positive number then the substring is present else the substring is not present.

string="StudyTonight"
substring="Stu"

print("Does ",substring," exist in ",string,"?")

# variable to store value returned by find()
v= string.find(substring)
if v>=0:
    print("Yes")
else:
    print("False")


Does Stu exist in StudyTonight ?
Yes

Approach 3: using contains()

In this approach, we will use the contains() method of an operator class. contains() function is used to test if a pattern of characters is contained within a string of a series. The function returns boolean values. To use the function we have to import the operator package in our program.

Algorithm

Follow the algorithm to understand the approach better.

Step 1- Import operator package

Step 2- Define a string with values

Step 3- Define substring which will be checked

Step 4- Check if the string contains the substring

Step 5- Print yes if it exists

Step 6- Else print no

Python Program 3

Look at the program to understand the implementation of the above-mentioned approach. We have used the in operator with the if conditional statement to check if the substring is a part of the string.

import operator

string="StudyTonight"
substring="night"

print("Does ",substring," exist in ",string,"?")

if operator.contains(string,substring):
    print("Yes")
else:
    print("No")


Does night exist in StudyTonight ?
Yes

Conclusion

We have discussed three different approaches for checking if a substring exists in a string in this tutorial. We have also discussed how to use functions of the string class and operator class in Python.