The Python find string function is used to return the index position of the first occurrence of a specified substring. It allows you to search and locate the position of a substring inside the original string. The find() function returns -1 if the specified string is not found.
In this article, we discuss how to write a Python string find() function with and without starting and ending index positions, with an example. The index position of the find() method starts from 0, not 1.
Python find() function syntax
The syntax of the string find() function is shown below.
String_Value.find(Substring, Starting_Position, Ending_Position)
- String_Value: A valid literal or original string where the search operation happens.
- Substring (required): The text that you want to search for or find inside the original string.
- Starting_Position (optional): If you want to specify the starting point (starting index position), where the search has to begin, specify the value here. If you omit this parameter, the string find function considers Zero as a starting position.
- Ending_Position (optional): If you want to specify the endpoint (Ending index position), where the search should stop, specify the value here. If you omit this parameter, the find method considers the highest number (last index position).
Return Value: The Python find() string function always returns an integer value as output.
- If the substring is found inside the original string. It returns the index position of the first character of the substring.
- If it is not found, the find() method returns -1 as output.
NOTE: The find() function returns the first occurrence of the substrings. If there are multiple occurrences, it returns the index position of the first character in the first occurrence.
Python find() string function examples
The following set of examples helps you understand the find string function.
Find a word in a string
In the following example, we declared a string with 7 words separated by a whitespace. The next line uses the find() string function to discover the first occurrence of the substring ‘abc’ inside Str1.
Str1 = 'We are abc working at abc company';
Str2 = Str1.find('abc')
print('First Output of a method is = ', Str2)
First Output of a method is = 7
In the code below, we used the find() function directly on the string to find the Tutorial index position.
# Performing directly
Str3 = 'Get Tutorial at Tutorial Gateway'.find('Tutorial')
print('Second Output of a method is = ', Str3)
Second Output of a method is = 4
Search from the start index position
Until now, we have used the find() method with a single parameter. However, the Python string find() function allows us to use the starting index position as the second argument. When we use this start value, we can increase the search performance because instead of searching from the first character, it will start looking for the specified position.
In the following example, we are searching for the same substring ‘abc’, but we specified the starting index position as 12. It means the search operation starts from the 12th position and continues until it finds the substring.
# Using First Index
Str1 = 'We are abc working at abc company';
Str5 = Str1.find('abc', 12)
print('Fourth Output = ', Str5)
Fourth Output = 22
NOTE: As the search starts at the 12th position, the substring abc at the 7th position is completely missed.
Python string find with start and end index
The built-in find() method allows us to use starting and ending indices. By providing the starting and ending index positions, we can increase the performance.
If the string length is 100 characters, and if we set the start and end indices to 10 and 22. Instead of searching 100 characters, the find() function starts at the 12th position and ends at the 21st position. The following Str6 Python statement starts looking for ‘abc’ from the 12th position. Please refer to the len() function.
# Using First & Second
Str6 = Str1.find('abc', 12, len(Str1) -1)
print('Fifth Output = ', Str6)
Fifth Output = 22
Python find() function search for a non-existent string
As we said earlier, if the find() function does not discover the specified substring inside the original string, it returns -1. Here, we search for the word ‘Tutorial’ inside a Str1 string, which is not present.
# Searching for Not existing Item
Str4 = Str1.find('Tutorial')
print('Third Output = ', Str4)
Third Output = -1
Similarly, the following code uses the find() function with start and end index positions and searches for non-existent text.
Here, the output of the Str7 is returning -1 because the string find() function starts looking from the 12th position (which means the first abc is skipped) and ends at index position 21. As we all know, the second abc is in position 22.
# Using First & Second while looking at a non-existing one
Str7 = Str1.find('abc', 12, 21)
print('Sixth Output of this method is = ', Str7)
Sixth Output of this method is = -1

Find a Character in a string
We can also use the find() method to search for a character within a string (word). If we pass a character as a parameter, it will search for the first occurrence of a character and return the index position.
s = 'hello'
print(s.find('l'))
2
Real-time examples and FAQs
Python string find case-insensitive
By default, the find() function is case-sensitive. When performing the search operation, the find() function considers ‘Hello’ and ‘hello’ as two distinct terms.
The following example returns -1 because, according to the find() method, both ‘Apple’ and ‘apple’ are different.
s = "Apple Phone"
print(s.find("apple"))
-1
To perform the string find() case-insensitive search operation, we must use either the lower() or upper() functions to convert the original string to common case. Next, apply the find() method on it.
s = "Apple Phone"
l = s.lower()
print(s.lower().find("phone"))
print(l.find("phone"))
6
6
Python string find last occurrence
As the find() function returns the first occurrence, there is a built-in rfind() method that returns the last occurrence of a substring in an original string.
s = "banana apple kiw apple orange"
print(s.find("apple"))
print(s.rfind("apple"))
7
17
Use find() to extract a substring or text
We can use the find() method to extract a substring or portion of the original string. The code below uses the find() function to extract the domain name from an email address.
email = 'contact@example.com'
pos = email.find('@')
print(email[pos + 1:])
example.com
Python find string in list
The for loop iterates over the list of strings, and the if statement with the find() function searches for the apple fruit. If it is found, print the item.
fruits = ['banana', 'apple', 'orange', 'apple', 'Pineapple']
for fruit in fruits:
if fruit.find('apple') != -1:
print(fruit)
apple
apple
Pineapple
Python string find all occurrences
As we all know, the find() function returns the first occurrence of the substring. However, there are situations where we need all occurrences. In such a case, we must use a loop to iterate over the string.
s = 'We abc working abc company abc period'
word = "abc"
positions = []
pos = s.find(word)
while pos != -1:
positions.append(pos)
pos = s.find(word, pos + 1)
print(positions)
[3, 15, 27]
Python string find() vs index() functions
Both the find() and index() functions are useful for search operations and locating a substring inside the original string.
When the string is found, both will return the index position of the starting character of the substring. However, the process and behaviour are different when the substring is not found.
- find(): It returns -1 when the substring is not found.
- index(): It raises a ValueError when the substring is not found.
s = "New Phone"
print(s.find("Phone"))
print(s.index("Phone"))
4
4
Let me try with the non-existing text or substring.
s = "New Phone"
print(s.find("Hi"))
print(s.index("Hi"))
-1
Traceback (most recent call last):
~~~~~~~^^^^^^
ValueError: substring not found
Difference between count()and find()
They both serve different purposes.
- find(): Search for a substring and returns the index position of the first occurrence.
- count(): It counts and returns the total number of occurrences.
s = 'We are abc working abc company abc'
print(s.find('abc'))
print(s.count('abc'))
7
3