Online Tutorials & Training Materials | STechies.com
Register Login

How to Check if a Python String Contains Another String?

|| 0

How to Check if a Python String Contains Another String?
Stechies

In this tutorial, you will learn various methods to check whether a string contains a different string.

Strings in Python are basically byte arrays that are used for representing Unicode characters. Strings are usually represented as letters surrounded by single or double quotation marks.  For example, “Hey” is a string.

Performing string operations are very common among coders. An important operation in Python is to check whether a string contains another string. This is possible in Python by using the in operator, contains function or the find () method. 

Let’s look these methods -      

Using “in” Operator

This is the best and one of the easiest method in python to check substring in string.

Example:

# Python program to check string in string
# Using in Operator

string = 'Hi this is STechies sites'

if 'STechies' in string:
    print('String found')
else:
    print('String not found')

Output:

String found

Note: “in” operator is case-sensitive

Returns: Returns True or False

If value exist, it will return True otherwise False.

Using find() Method

Python find() method checks for the string in string and returns an integer value, if string exists it returns the index of first occurrence of string or else return -1.

Note: “in” operator is case-sensitive

Returns: integer value

Does not throw exceptions error

Example:

# Python program to check string in string
# Using find method

string = 'Hi this is STechies sites'

if string.find('STechies') != -1:
    print('String found')
else:
    print('String not found')

Output:

String found

Using String contains __contains__()

Python __contains__() is similar to “in” method which checks the string is present in the string and return boolean value True or False if value present it returns True or else False.

Example:

# Python program to check string in string
# Using __contains__ function

string = 'Hi this is STechies sites'

if string.__contains__('STechies'):
    print('String found')
else:
    print('String not found')

Output:

String found

Note: “__contains__” function is case-sensitive

Returns: Returns True or False

Using Regular Expressions (REGEX)

Python comes with an in-build library which is known as Regular Expressions, denoted as “re”, we need to just include this library at the top of the program.

With the help of these regular expressions we can do a complex search for a substring in the string

To do the search of string in the string we can use function “search” in module “re”.

Example:

# Python program to check string in string
# Using Regular Expressions

import re

string = 'Hi this is STechies site'

if re.search("STechies", string):
    print('String found')
else:
    print('String not found')

Output:

String found

Note: “re.search” function is case-sensitive

Search String in String with Non-Case Sensitive or Case Insensitive Python

As you have seen that all of the above methods are case sensitive, to search the string in a string with case insensitive we need to create a custom function.

Example:

# Python program to check string in string
# Using in Operator

# Initiate string
string = 'Hi this is STechies sites'

# Initiate substring to search
substring = 'stechies'

# Convert both string and substring to lower case
string = string.lower()
substring = substring.lower()

# Check if substring present in main string
if substring in string:
    print('String found')
else:
    print('String not found')

Output:

String found

In the above example, first we have converted string and sub-string to lowercase and then check if sub-string present in the main string.