How to check if a string contains a substring in python?

A substring is a sequence of characters within another string. Consider a string string_1 = 'Program'. 'pro', 'ogr', 'gram' are the substrings of string_1. This article discusses different approaches to check if a substring is present in a string or not.

Checking if a substring is present in a string using the find() function

The find() function is a built-in function in python that returns the first position of the specified value. The find() method takes three arguments value, start(optional), and end(optional). The value represents the substring or a character to search for. The find() function starts searching from the starting index and stops searching at the ending index value.

If the substring is not present, the find() function returns -1 as output.

For example, Consider a string string_1 ='Program'. If we want to find substring 'gram' using find() method from the string_1 then find() method returns the index position of substring which is 3.

string_1 = "Program"
print("string_1: ", string_1)
print("Position of 'gram' : ", string_1.find('gram'))
if string_1.find('gram') >= 0:
    print("substring Found")
else:
    print("Not found")

Output

string_1:  Program
Position of 'gram' :  3
substring Found

Checking if a substring is present in a string using the count() method

The count() function returns the number of occurrences of a specified element. The count() function takes an element of the iterable as a parameter. If the element is not present in the iterable count() function returns 0.

Consider a string string_1 ='Program'. The count() function takes substring 'gram' as a parameter and returns the number of occurrences as 1. Hence, substring 'gram' is present in 'Program'.

string_1 = "Program"
print("string_1: ", string_1)
print("No. of occurrences of 'gram': ", string_1.count('gram'))
if string_1.count('gram') > 0:
    print("substring Found")
else:
    print("Not found")

The above code returns the output as,

string_1:  Program
No. of occurrences of 'gram' :  1
substring Found

Checking if a substring is present in a string using the re.search() method

The re.search() method searches for a pattern in a string. The re.search() method can be imported from the re(Regular expressions) module. The re.search() returns the first match of the substring.

If the substring is not found in the original string, the re.seach() returns a None value. The syntax for re.search() is given below.

#syntax:
re.search(pattern, string)
  • pattern: The pattern indicates the substring to search for.
  • string: This represents the original string.

Consider a string string_1 ='Program'. re.search('gram', string_1) returns the span as (3, 7).

import re
string_1 = "Program"
print("string_1: ", string_1)
print("Position of substr 'gram':", (re.search('gram', string_1)))
if (re.search('gram', string_1)):
    print("substring Found")
else:
    print("Not found")

The above code returns the output as

string_1:  Program
Position of substr 'gram': <re.Match object; span=(3, 7), match='gram'>
substring Found

Checking if a substring is present in a string using in operator

The in operator checks the existence of an item in an iterable and returns a boolean value. Consider a string string_1 = Welcome To Python. The below code checks if substring 'come' is present in string_1 using in operator.

string_1 = "Welcome To Python"
print("string_1: ", string_1)
if "come" in string_1:
     print("substring Found")
else:
    print("Not found")

Output

string_1:  Welcome To Python
substring Found

Checking if a substring is present in a string using the index() method

The index() is a built-in method that takes an item of an iterable as an argument and returns the first position at which the item was encountered. If the item doesn't exist in the iterable it raises a ValueError exception.

Consider a string string_1 = Welcome To Python. We use try and except block to handle errors in code. If the index() method raises ValueError except block is executed and output is printed.

Example 1: checking for the existence of substring 'come' in string_1.

string_1 = "Welcome To Python"
print("string_1: ", string_1)
try:
     print("Index value: ", string_1.index('come'))
     print("substring Found")
except:
     print("Not found")

The above code returns the output as

string_1:  Welcome To Python
Index value:  3
substring Found

Example 1: checking for the existence of substring 'Well' in string_1.

string_1 = "Welcome To Python"
print("string_1: ", string_1)
try:
     print("Index value: ", string_1.index('Well'))
     print("substring Found")
except:
     print("Not found")

The above code returns the output as

string_1:  Welcome To Python
Not found

Checking if a substring is present in a string using the operator.contains() method

In python, contains() is a built-in method in the operator module. The contains() method takes two parameters a sequence(list, tuple, set, string, etc), and a data item, and returns a boolean value as output. The syntax for the contains() method is given below.

#syntax:
operator.contains(sequence, item)

Consider a string string_1 = Welcome To Python. operator.contains(string_1, 'come') checks if come is present in string_1. If the contains() method returns True if block is executed and output is printed.

import operator
string_1 ="Welcome To Python"
print("string_1: ", string_1)
if (operator.contains(string_1, 'come')):
    print("substring Found")
else:
    print("Not found")

Output

string_1:  Welcome To Python
substring Found
0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions