Python String Find()

Meenakshi Agarwal
By
Meenakshi Agarwal
Hi, I'm Meenakshi Agarwal. I have a Bachelor's degree in Computer Science and a Master's degree in Computer Applications. After spending over a decade in large...
7 Min Read
Python String Find with Examples

This tutorial explains the Python string find() method that searches a substring and returns the index of its first occurrence.

If not successful, it returns the status as (-1). Let’s try to understand its usage with the help of simple examples.

Python String Find() with Examples

String’s find() is a built-in Python function that finds a substring in another string that contains it. If successful, it returns the index of the first match. Otherwise, the function returns a (-1) to mark the failure.

Let’s now see the details and check out how can we use it.

String Find() Syntax

The basic syntax to use Python string find() is as follows:

Given_string.find(Sub_string, Begin_index, End_index)

Parameters

Below are the descriptions of the parameters in the above function.

  • Given_string: It is the string object that we’ve to traverse.
  • Sub_string: It is the substring argument that we want to find.
  • Begin_index: It is the starting index to begin the search in our input string. It is an optional parameter. And, if we don’t specify it, then find() takes zero as the default.
  • End_index: It tells the find() function where to end the search. It is also an optional argument. And, if we don’t specify it, then the highest index number becomes the default.

Return Values

The find() function may have one of the following return values.

  • If the string contains the substring, then the return value is the index of the first occurrence.
  • If the substring doesn’t match, then the return value is -1.

Note:- The first occurrence is the lowest index when the substring occurs twice or more times in the source string.

Also Read: Python String Strip

Find() Examples

Here, we are using string find() to address different use cases in Python. Hope, these should help you with this function from all corners.

Find() function with default values

In this example, we’ve not passed any values in the find() call. So, it will assume the default values for beginning and ending indices.

As stated initially, the default for begin_index is 0, and for end_index is the length of the string.

"""
 Desc:
  Python example to demonstrate string find() function
"""

in_str = 'Does Python supports Lambda function?'
find_str = 'supports'

ret_index = in_str.find(find_str)

print(f"The input string: {in_str}")
print(f"The position of '{find_str}' substring is: ", ret_index)

After executing the above snippet, you will see this result:

The input string: Does Python supports Lambda function?
The position of 'supports' substring is: 12

We’ve used Python f-string in all examples given here. Hence, make sure you run these using Python 3.6.

The string contains two or more substrings

As stated earlier, the string find() returns the first index in case of multiple occurrences of a substring. Here’s an example to see this behavior:

"""
 Desc:
  Using find() when the string has two or more occurrences of a substring
"""

in_str = 'The most popular Python versions are Python 2.6, and Python 3.6.'
find_str = 'Python'

ret_index = in_str.find(find_str)

print(f"The input string is: {in_str}")
print(f"The position of '{find_str}' substring is: {ret_index}")

This code would give you the following result:

The input string is: The most popular Python versions are Python 2.6, and Python 3.6.
The position of 'Python' substring is: 17

Also Check: Python Ord() Function All You Need to Know

When find() doesn’t find a substring

Let’s understand – what does Python find() returns when the given string doesn’t contain the required substring? Usually, in this case, the function should fail and return -1. Here’s an example to observe this behavior:

"""
Desc:
Using find() when the string doesn't contain the substring
"""

in_str = 'The most popular Python versions are Python 2.6, and Python 3.6.'
find_str = 'CSharp'

ret_index = in_str.find(find_str)

print(f"The input string is: {in_str}")
print(f"The position of '{find_str}' substring is: {ret_index}")

After running this code, you will get the following output:

The input string is: The most popular Python versions are Python 2.6, and Python 3.6.
The position of 'CSharp' substring is: -1

Find() string with begin and end indices

When you supply the two optional arguments of the find() method. Then, it searches the substring in a particular range, i.e., between (start, end).

Let’s see how to use the starting and ending indices with the find() function. The below example demonstrates how to find a substring within a string in Python.

"""
Desc:
Using str.find(str, start, end) to find substring in sliced string
"""

in_str = 'The most popular Python versions are Python 2.6, and Python 3.6.'
find_str = 'Python'

start = in_str.find(find_str) + len(find_str)
end = len(in_str)

ret_index = in_str.find(find_str, start, end)

print(f"The input string is: {in_str}")
print(f"The position of '{find_str}' in sliced string '{in_str[start:end]}' is: {ret_index}")

In this Python string example, we tried to find the index of the second occurrence of our substring. That’s why we found the first index and incremented it by the substring’s length. After running the given snippet, you should see the following result:

The input string is: The most popular Python versions are Python 2.6, and Python 3.6.
The position of 'Python' in sliced string ' versions are Python 2.6, and Python 3.6.' is: 37

Must Read –
How to Use Regular Expression in Python
List Index Method in Python
How to Slice a String in Python with 6 Simple Methods
How to Use String indexOf in Python

Before You Leave

Congratulations! You finally completed this tutorial successfully.

We hope you now feel confident using the Python string find() method to find substrings in strings. Be sure to check out our other tutorial on concatenating strings in Python.

Also, to learn Python from scratch to depth, read our step-by-step Python tutorial.

Lastly, our site needs your support to remain free. Share this post on social media (Linkedin/Facebook) if you gained some knowledge from this tutorial.

Enjoy coding,
TechBeamers.

Share This Article
Subscribe
Notify of
guest

0 Comments
Newest
Oldest
Inline Feedbacks
View all comments