Python Check if String Contains Substring

In this tutorial, we will look at how to check if a string contains a substring in Python. A substring is a sequence of characters in a given string. Python has several built-in methods that can help find and replace a substring.

Following are the methods available in Python to check if a string contains a substring.

  1. using find() method
  2. using in operator
  3. using count() method
  4. using str.index() method
  5. using operator.contains() method

Check if String Contains Substring in Python

Let’s look at all the different ways in Python to check if a string contains a substring.

Method 1 – Using the find() method 

find() method is used to check if a Python string contains a substring or not. If the string contains a substring, it returns the starting index of the substring; else returns -1 if it cannot find a substring.

Syntax:

string.find(substring, start, end)

Parameters: 

  • substring: substring that needs to be searched in a given string
  • start (Optional): Starting position where the substring needs to be searched within the string. 
  • end(Optional): Ending position where suffix needs to be searched within the string. 

Note: If start and end indexes are not provided, then by default, it takes 0 as the start index and length-1 as the end index.

word = 'Hello its a beautiful day'

# returns first occurrence of Substring
result = word.find('beautiful ')
print ("Substring 'beautiful ' found at index:", result )  

# Substring is searched with start index 2
print(word.find('its', 2)) 

# Substring is searched with start index 10
print(word.find('its', 10)) 

# Substring is searched between start index 4 and end index 10
print(word.find('its', 4, 10)) 

# Substring is searched start index 10 and end index 20
print(word.find('its ', 10, 20))

# How to use find()
if (word.find('sunny') != -1):
    print ("Contains given substring ")
else:
    print ("Doesn't contains given substring")

Output

Substring 'beautiful ' found at index: 12
6
-1
6
-1
Doesn't contains given substring

Method 2 – Using the in operator

The “in” operator checks if a substring is present within a string, returns TRUE if found else returns FALSE.

word = "Hello its a beautiful day"
sub1="beautiful"
sub2="sunny"

print(sub1 in word) 
print(sub2 in word)

#Output
True
False

Method 3 – Using the count() method

The count() method checks for the occurrence of a substring in a string; if not found, return 0.

word = "Hello its a beautiful day"
sub1="beautiful"
sub2="Hello"
sub3="Sunny"

print(word.count(sub1)) 
print(word.count(sub2)) 
print(word.count(sub3)) 

# Output
1
1
0

Method 4 – Using the str.index() method

The method checks that the given substring is present in a string. If the substring is not present in the string, it doesn’t return any value rather generates a ValueError.

Syntax:

string.index(substring)
word = "Hello its a beautiful day"
try :  
    result = word.index("beautiful") 
    print ("beautiful is present in the string.") 
except : 
    print ("beautiful is not present in the string.") 

# Output
beautiful is present in the string.

Method 5 – Using the operator.contains() method

Using the operator module, we can search if the substring is present in a string.

Syntax:

operator.contains(string,substring)
import operator 

word = "Hello its a beautiful day"
if operator.contains(word, "beautiful"): 
    print ("beautiful is present in the string.") 
else : 
    print ("beautiful is not present in the string.")

# Output
beautiful is present in the string.
Leave a Reply

Your email address will not be published. Required fields are marked *

Sign Up for Our Newsletters

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

You May Also Like