Python String find()

Python String find() method

Python String find() method is used to find the index of first occurrence of a specific value in the given string.

Consider that given string is in x.

x = "Some apples are red. Some apples are good."

Then the return value of x.find(“apple”) is

5

Because, the first occurrence of given value "apple" is at index=5 in the given string.

Syntax of find() method

The syntax of String find() method in Python is given below.

string.find(value, start, end)

The find() method takes three parameters.

ParameterDescription
value[Mandatory]
A string value.
The value which has to be found in the given string.
start[Optional]
An integer value.
Specifies the starting index, from where to start the search for specified value in the string.
The default value is 0.
end[Optional] Can be specified only if start is specified.
An integer value.
Specifies the ending index, where to stop the search for specified value in the string.
The default value is end of the string.

find() method returns an integer value representing the index of the first occurrence of the value with optional bounds [start, end] in the given string.

If there is no occurrence of the value in the given string, then find() returns -1.

The searching of the value in the given string happens from left to right, i.e., from starting of the string to ending of the string.

Examples

1. find(value) – Find the index of occurrence of value in given Python string

In the following program, we take a string in variable x, and find the index of first occurrence of the value “apple” in the string using String find() method.

We shall store the find() returned value in result variable and print it to the standard output.

Python Program

x = "Some apples are red. Some apples are good."
result = x.find("apple")
print(f"Index : {result}")
Run Code Copy

Output

Index : 5

2. find(value, start) – Find the index of occurrence of value in given Python string from a specific start

In the following program, we take a string in variable x, and find the index of occurrence of the value “apple” in the given string x from a start index of 10 using String find() method.

Python Program

x = "Some apples are red. Some apples are good."
result = x.find("apple", 10)
print(f"Index : {result}")
Run Code Copy

Output

Index : 26

3. find(value, start, end) – Find the index of occurrence of value in given Python string in specified bounds

In the following program, we take a string in variable x, and find the index of occurrence of the value “apple” in the given string x from a start index of 10 and end index of 30 using String find() method.

Python Program

x = "Some apples are red. Some apples are good."
result = x.find("apple", 10, 35)
print(f"Index : {result}")
Run Code Copy

Output

Index : 26

4. find() – Value not present in the string

If the specified value is not present in the string, then find() method returns -1, as shown in the following program.

Python Program

x = "Some apples are red. Some apples are good."
result = x.find("banana")
print(f"Index : {result}")
Run Code Copy

Output

Index : -1

Summary

In this tutorial of Python String Methods, we learned about String find() method, its syntax, and examples.

Related Tutorials

Code copied to clipboard successfully 👍