How to Check if a Python String Contains Another String?

Posted in

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

Vinay Khatri
Last updated on February 11, 2025

A string is a collection of Characters and in Python, creating and manipulating a string is very easy as compared to other programming languages. Python also provides us with many string methods that can be used to perform various operations on a string.

While learning the Python string topic, you will defiantly come across a general problem of finding a particular piece of string present in a longer string. This problem state that we have two strings One is the main string, and the other is the substring, and using the code, we have to create a program that can tell whether the substring is present in the main string or not. Here we have provided different ways to solve this problem:

Python in Operator

With the help of Python in operator, we can find whether a piece of string is present in another string or not, in is a list operator, which is generally used to tell that a specific element is present in the list or not.

Example:

main_string = "Welcome to TechGeekBuzz"
sub_string = "Tech"

# condition to check whether the Tech string is present in the Welcome to TechGeekBuzz string
if sub_string in main_string: 
    print("Present")
else:
    print("Not Present")

Output:

Present

Using find() Method:

The find() method is used to find the index value of a particular element present in the list or string. If the element is present in the list, the find() method returns its index value, else it returns a negative number -1.

>>> string = "This is TechGeekBuzz"
>>> string.find("T")  # here it will find the character T
0
>>> string.find("Tech")
8
>>> string.find("Python")
-1

Example:

main_string = "Wellcome to TechGeekBuzz
sub_string = "Tech"

# condition to check whether the Tech string is present in the Wellcome to TechGeekBuzz string
if main_string.find(sub_string)>=0: 
    print("Present")
else:
    print("Not Present")

Output:

Present

Using index() Method:

The index() method is used to tell the index value of a specific element present in the list or string. It is similar to the find() method, but if the index() method does not find the element in the string, it throws an error, or else it returns the beginning index value of the element.

Example

main_string = "WelCome to TechGeekBuzz"
sub_string = "python"

# condition to check whether the Tech string is present in the WelCome to TechGeekBuzz string
try:
    main_string.index(sub_string)
    print("Present")
except:
    print("Not Present")

Output

Not Present

Using Python Regular Expression

Regular Expression is used to find the pattern in a string, and they provide a more flexible and efficient way to find a substring from a string. With regular expression, we have many string operations that we can perform on a string.

Example

from re import search

main_string = "Wellcome to TechGeekBuzz"
sub_string = "Tech"

# condition to check whether the Tech string is present in the Wellcome to TechGeekBuzz string
if search(sub_string,main_string):
    print("Present")
else:
    print("Not Present")

Output

Present

Using Logical Code

It might be possible you have asked to implement a logic to solve this problem rather than using the special Python methods. Here we have also provided a logic to solve this problem using python code:

Example

def check_string(s1, s2):
    M = len(s1)
    N = len(s2)

    for i in range(N - M + 1):
        for j in range(M):
            if (s2[i + j] != s1[j]):
                break         
        if j + 1 == M :
            return i
    return -1

sub_string = "Tech"
main_string = "Welcome to TechGeekBuzz"
result = check_string(sub_string, main_string)
if result == -1 :
    print("Not present")
else:
    print("Present")

Output

Present

Conclusion

The logic we have used in the last example is applicable to every programming language. In competitive programming, we often encounter such kind of problem where we are restricted to using some of the inbuilt functions or methods there we need to implement logic using the basic programming data structure and syntax.

People are also reading:

Leave a Comment on this Post

0 Comments