Check if a substring exists in a String in Python

In this tutorial, you will how to check if a String contains a substring or not in Python.

Table Of Contents

Introduction

A string is a set of characters, and a substring is also a set of characters but it resides in another string.

For example, we have a string “Welcome to thisPointer”. Now we want to check if a substring “Welcome” is present in the original string or not. Let’s discuss some approaches how to check if a string contains a substring in python.

Check if a String contains a substring using the find() method

The string’s find() method will return the index of the first position of the substring in the actual string. If the substring is not found, then it will return -1.

Syntax:

input_str.find(substring)

Parameters:

  • It takes substring as a parameter.
  • The input_str is the actual input string.

It will return the index position of first occurrence of given substring in the string. If given substring does not exist in the string, then it will return -1. We can use this to check if a given substring exists in a string or not.

Example 1:

In this example, we will check if the substring-‘to’ exists in a string or not.

input_str="Welcome to thisPointer"

# Check for substring-'to' in the string
pos = input_str.find('to')
if pos > -1:
    print('Substring is present in the string, at index position: ', pos)
else:
    print('Substring is not present in the string')

Output:

Substring is present in the string, at index position:  8

You can see that the substring ‘to’ is present at the 8th position in the actual input string – “Welcome to thisPointer”.

Example 2:

In this example, we will check if the substring-‘hello’ exists in a string or not.

input_str="Welcome to thisPointer"

# Check for substring-'hello' in the string
pos = input_str.find('hello')
if pos > -1:
    print('Substring is present in the string, at index position: ', pos)
else:
    print('Substring is not present in the string')

Output:

Substring is not present in the string

You can see that the substring ‘hello’ is not present in the actual input string – “Welcome to thisPointer”. So it returned -1.

Check if a String contains a substring using the count()

The string’s count() method in Python will return the total count of substrings found in the actual string. It will return at least 1 if the substring is found. If the substring is not found, it will return 0

Syntax:

input_str.count(substring)

It takes substring as a parameter and returns the number of occurrences of given substring in the string. We can use this to check if a given substring exists in a string or not. For that, just check if the value returned by count() is more than 0.

Example 1:

In this example, we will check for the substring-‘to’.

input_str="Welcome to thisPointer"

# Check for substring-'to' in the string
freq = input_str.count('to')

if freq > 0:
    print('Substring is present in the string. Occurrence count is: ', freq)
else:
    print('Substring is not present in the string')

Output:

Substring is present in the string. Occurrence count is:  1

You can see that the substring ‘to’ occurs one time in the actual input string – “Welcome to thisPointer”. So it returned 1.

Example 2:

In this example, we will check for the substring-‘hello’.

input_str="Welcome to thisPointer"

# Check for substring-'hello' in the string
freq = input_str.count('hello')

if freq > 0:
    print('Substring is present in the string. Occurrence count is: ', freq)
else:
    print('Substring is not present in the string')

Output:

Substring is not present in the string

You can see that ‘hello’ is not present in the actual input string – “Welcome to thisPointer”. So it returned 0.

Check if a String contains a substring using the operator.contains()

The contains() python method is available in operator module. It returns True if the substring is present in the given actual string, otherwise returns False.

Syntax:

operator.contains(input_str, substring)

Parameters:
It takes input_str as the first parameter and substring as the second parameter.
1. input_str the actual string
2. substring is the substring.

If the substring exists in the input_str, it will return True, else False.

Example 1:

In this example, we will check for the substring-‘to’.

import operator

input_str="Welcome to thisPointer"

# Check for substring-'to' in the string
if operator.contains(input_str, "to"):
    print('Substring is present in the string.')
else:
    print('Substring is not present in the string.')

Output:

Substring is present in the string.

You can see that ‘to’ is present in the actual input string – “Welcome to thisPointer”. So it returned True.

Example 2:

In this example, we will check for the substring-‘hello’.

import operator

input_str="Welcome to thisPointer"

# Check for substring-'hello' in the string
if operator.contains(input_str, "hello"):
    print('Substring is present in the string.')
else:
    print('Substring is not present in the string.')

Output:

Substring is not present in the string.

You can see that ‘hello’ is not present in the actual input string – “Welcome to thisPointer”. So it returned False.

Check if a String contains a substring using in operator.

The in operator in Python, is the membership operator. It returns True if the substring is present in the given actual string. otherwise False is returned.

Syntax:

substring in input_str

Where,
1. input_str the actual string
2. substring is the substring.

Returns True if the substring is present in the input_str, else returns False.

Example 1:

In this example, we will check for the substring-‘to’.

input_str="Welcome to thisPointer"

# Check for substring-'to' in the string
if "to" in input_str:
    print('Substring is present in the string.')
else:
    print('Substring is not present in the string.')

Output:

Substring is present in the string.

You can see that ‘to’ is present in the actual input string – “Welcome to thisPointer”. So it returned True.

Example 2:

In this example, we will check for the substring-‘hello’.

input_str="Welcome to thisPointer"

# Check for substring-'hello' in the string
if "hello" in input_str:
    print('Substring is present in the string.')
else:
    print('Substring is not present in the string.')

Output:

Substring is not present in the string.

You can see that ‘hello’ is not present in the actual input string – “Welcome to thisPointer”. So it returned False.

Summary

In this article, we have seen how to check if a string contains a substring using four methods. The find() will return the index position of the substring if it is found, otherwise, it will return -1. The count() will return the substring occurrence count if it is found, otherwise, 0 is returned. The in and operator.contains() return True if the substring is found in the input string, otherwise False is returned. Happy Learning.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top