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

Finding substring in a string is one of the most commonly faced problem. Traditional way to find out 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 characters 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 file that requires to find 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 lowest 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 sub-string 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 with an example of how it works ( You can 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 occurrence index of a substring. Try to find out the total number of occurrence of a substring in a string. You can take help of 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, 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 passing the startIndex and endIndex to the find() method.

You might also like :

Categories: python

Leave a Reply

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