Python program to find a substring in a string

Python program to find a substring in a string using find() :

Finding substring in a string is one of the most commonly faced problems. The traditional way to find out a substring is by using a loop. We can use one loop and scan each character of the main string one by one. If any character is found equal to the first character of the substring, compare each subsequent character to check if this is the start point of the substring in the string or not.

This approach will take a lot of time and also we don’t want to implement this method for every single Python file that needs to find a substring.

Python comes with a lot of useful inbuilt methods. find() method in python is used to find out a substring in a string.

This method will find the index of a substring in a string and returns the index of the first occurrence of the substring. If the substring is not found, it will return -1. Note that it will return only the first occurrence or smallest index of the substring. If the string has two occurrences, we will get the index only for the first occurrence.

The syntax of the method is as below :

python find substring

  • substring is the substring to find in the str string.
  • startIndex is the starting index of the string where the search should begin. By default, its value is 0 i.e. the search will start at the start index of the string by default.
  • endIndex is the end index of the string i.e. where the search should stop. Its value is equal to the length of the string by default, i.e. the search will try to find the substring to the end of the main string.

Example :

Let me show you an example of how it works

given_str = "The quick brown fox jumps over the lazy dog"


sub_string = input("Enter a sub-string to find in the above string : ")

if(given_str.find(sub_string) == -1):
    print("The substring doesn't contain in the string")
    print("The substring is found on position : ",given_str.find(sub_string))

( You can also download the code from here):

python find substring

Sample Output :

python find substring

ToDo :

We can use ‘find’ to create different types of applications.

  1. find() returns only the first index of a substring. Try to find out the total number of occurrences of a substring in a string. You can use the start index and end index. If a substring is found, start scanning the string starting from the end index of that substring.
  2. Find all substrings in a file. Open the file, read each line one by one and check for the substring in each line.
  3. Find if a keyword or substring exists in a webpage or not. You need to learn about python web scraping for that. Many great libraries are available for scraping web pages (Beautiful Soup is a popular one). Ask the user to enter the web page address and keyword. Scrap that web page (if available) and print out if the keyword is available or not.

These three are simple use-cases of find() method that came to my mind. Try to implement these problems. If you have anything in your mind, please don’t hesitate to drop a comment below.

Conclusion :

In this tutorial, we have learned how to find one substring in a string in python using the find() method. Try to run the above example and also try to find a substring in a given range by using the startIndex and endIndex with the find() method.

Similar tutorials :