Python String find() Method with Examples
⚡ Smart Summary
Python string find() is a built-in method that returns the index of the first occurrence of a substring within a string. It returns -1 when the substring is not present, avoiding exceptions.
What is Python String find()?
Python String find() is a function available in the Python library to find the index of the first occurrence of a substring within a given string. The find() function returns -1 instead of throwing an exception when the specified substring is not present in the given string. It is one of Python’s most-used string methods for locating text.
Syntax of Python string find()
The basic syntax of the Python find() function is as follows:
string.find(substring,start,end)
Parameters for the find() method
Here are the three parameters of the find() function in Python:
- substring: The substring you want to search for in the given string.
- start: (optional) The start value from where the search for the substring will begin. By default, it is 0.
- end: (optional) The end value where the search for the substring will end. By default, the value is the length of the string.
Example of find() method with default values
The parameters passed to the Python find() method are the substring, that is, the string you want to search for, plus start and end. The start value is 0 by default, and the end value is the length of the string.
In this example, we will use the find() method in Python with default values.
The find() method will search for the substring and give the position of the very first occurrence of the substring. If the substring is present multiple times in the given string, it will still return the index or position of the first one.
Example:
mystring = "Meet Guru99 Tutorials Site.Best site for Python Tutorials!"
print("The position of Tutorials is at:", mystring.find("Tutorials"))
Output:
The position of Tutorials is at: 12
Example of find() using start argument
You can search for the substring in the given string and specify the start position from where the search will begin. The start parameter can be used for this purpose.
The example below specifies the start position as 20, and the find() method in Python will begin the search from position 20. Here, the end position will be the length of the string, so it searches till the end of the string from position 20 onwards.
Example:
mystring = "Meet Guru99 Tutorials Site.Best site for Python Tutorials!"
print("The position of Tutorials is at:", mystring.find("Tutorials", 20))
Output:
The position of Tutorials is at 48
Example of find() using start and end arguments
Using the start and end parameters, we can limit the search instead of searching the entire string.
Example:
mystring = "Meet Guru99 Tutorials Site.Best site for Python Tutorials!"
print("The position of Tutorials is at:", mystring.find("Tutorials", 5, 30))
Output:
The position of Tutorials is at 12
Example of find() method to find the position of a given substring in a string
We know that find() helps us find the index of the first occurrence of a substring. It returns -1 if the substring is not present in the given string. The example below shows the index when the string is present and -1 when the substring we are searching for is not found.
Example:
mystring = "Meet Guru99 Tutorials Site.Best site for Python Tutorials!"
print("The position of Best site is at:", mystring.find("Best site", 5, 40))
print("The position of Guru99 is at:", mystring.find("Guru99", 20))
Output:
The position of Best site is at: 27 The position of Guru99 is at: -1
Python string rfind()
The Python rfind() function is similar to the find() function, with the only difference being that rfind() gives the highest index for the given substring, while find() gives the lowest, that is, the very first index. Both rfind() and find() return -1 if the substring is not present.
In the example below, we have a string “Meet Guru99 Tutorials Site. Best site for Python Tutorials!” and will try to find the position of the substring Tutorials using find() and rfind(). The substring Tutorials occurs twice in the string.
Here is an example where both find() and rfind() are used.
mystring = "Meet Guru99 Tutorials Site.Best site for Python Tutorials!"
print("The position of Tutorials using find() : ", mystring.find("Tutorials"))
print("The position of Tutorials using rfind() : ", mystring.rfind("Tutorials"))
Output:
The position of Tutorials using find() : 12 The position of Tutorials using rfind() : 48
The output shows that find() gives the index of the very first Tutorials substring it finds, while rfind() gives the last index of the substring Tutorials.
Python string index()
The Python string index() is a function that gives you the position of the given substring, just like find(). The only difference between the two is that index() throws an exception if the substring is not present in the string, while find() returns -1.
Here is a working example that shows the behaviour of index() and find().
mystring = "Meet Guru99 Tutorials Site.Best site for Python Tutorials!"
print("The position of Tutorials using find() : ", mystring.find("Tutorials"))
print("The position of Tutorials using index() : ", mystring.index("Tutorials"))
Output:
The position of Tutorials using find() : 12 The position of Tutorials using index() : 12
We get the same position for both find() and index(). Let us see an example when the given substring is not present in the string.
mystring = "Meet Guru99 Tutorials Site.Best site for Python Tutorials!"
print("The position of Tutorials using find() : ", mystring.find("test"))
print("The position of Tutorials using index() : ", mystring.index("test"))
Output:
The position of Tutorials using find() : -1
Traceback (most recent call last):
File "task1.py", line 3, in <module>
print("The position of Tutorials using index() : ", mystring.index("test"))
ValueError: substring not found
In the above example, we are trying to find the position of the substring “test”. The substring is not present in the given string, so using find() we get the position as -1, but for index() it throws an error, as shown above.
To find the total occurrence of a substring
To find the total number of times a substring has occurred in the given string, we will make use of the find() function in Python. We will loop through the string using a for-loop from 0 till the end of the string, making use of the startIndex parameter for find().
The variables startIndex and count will be initialized to 0. Inside the for-loop, we will check whether the substring is present inside the given string using find() with startIndex as 0.
The value returned from find(), if not -1, will update startIndex to the index where the string is found and also increment the count value.
Here is the working example:
my_string = "test string test, test string testing, test string test string"
startIndex = 0
count = 0
for i in range(len(my_string)):
k = my_string.find('test', startIndex)
if(k != -1):
startIndex = k+1
count += 1
k = 0
print("The total count of substring test is: ", count )
Output:
The total count of substring test is: 6
» Learn more about Python String split()

