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.

  • 🔍 Syntax: find() takes a substring plus optional start and end indexes to limit the search range.
  • 🎯 First match: find() returns the lowest index of the first occurrence, scanning left to right.
  • Not found: find() returns -1 rather than raising an error when the substring is absent.
  • 🔁 rfind(): The rfind() method returns the highest index, searching from the right side instead.
  • ⚠️ index(): The index() method behaves like find() but raises a ValueError when the substring is missing.
  • 🤖 AI assistance: AI coding assistants generate find() logic and power substring search in NLP text pipelines.

Python String find()

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()

FAQs

Yes. find() matches the substring exactly, so “Hello”.find(“h”) returns -1 while “Hello”.find(“H”) returns 0. Convert the string with lower() or upper() first when you need a case-insensitive search.

The in operator returns only True or False to show whether a substring exists. find() goes further and returns the exact index of the first match, or -1 when the substring is absent.

Yes. find() accepts negative start and end indexes, which count from the end of the string. For example, “Guru99”.find(“9”, -2) searches only the last two characters and returns their position.

The str.find() method locates one match at a time. For all matches in a single call, use the re module: re.findall() returns every matching substring, and re.finditer() yields match objects with each start position.

find() is a string-only method, so calling it on a list raises an AttributeError. To locate an item in a list or tuple, use the index() method instead, which returns the position of the first match.

Yes. For a simple substring search, find() is implemented in C and runs faster than compiling a regular expression. Reach for the re module only when you need patterns, wildcards, or multiple matches.

In machine learning and NLP, find() locates keywords, tags, or delimiters while cleaning and tokenizing text. Detecting whether a substring is present, or where it sits, helps build features and parse model inputs.

Yes. GitHub Copilot autocompletes find(), rfind(), and index() calls from a short comment describing your goal. Agentic AI tools go further, writing the search logic, adding start and end arguments, and running your tests.

Summarize this post with: