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 :
– 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.
Let me show you with an example of how it works ( You can download the code from here):
Sample Output :
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 :
- 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