Search Substring In A Given Python String

Search Substring In A Given Python String

Published on October 21 2021

In this tutorial, you will understand how to search a substring in python. There are multiple ways, we will do by using the find() method from python.

Source code

sentence = "my name is roshan"
substring = "john"

def SearchString(substring):
    result = sentence.find(substring) #start index, last index
    if(result == -1):
        print("Given substring not found.")
    else:
        print("substring found at index : ", result)

SearchString(substring)

Video

 



Next Tutorial

  • Search Substring In A Given Python String