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.
- 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.
Let me show you with an example of how it works
given_str = "The quick brown fox jumps over the lazy dog" print(given_str) 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") else: print("The substring is found on position : ",given_str.find(sub_string))
( You can also download the code from here):
We can use ‘find’ to create different types of applications.
- 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.
- Find all substrings in a file. Open the file, read each line one by one and check for the substring in each line.
- 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.
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.
- Python program to sort words of a string in alphabetical order
- Python 3 program to check if a string is pangram or not
- Python 3 program to count the total number of characters in a string
- Find total number of lowercase characters in a string using Python 3
- Python program to remove characters from odd or even index of a string
- Python program to replace character in a string with a symbol