Python program to find a substring in a string

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

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.
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.
endIndex is the end index of the string i.e. where the search should stop.

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

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 :

Leave a Reply