Python String index()


Python String index() method

Python String index() method is used to get 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.index("apple") is

5

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

Syntax of index() method

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

string.index(value, start, end)

The index() 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.

index() 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 index() method raises ValueError.

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. index(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 index() method.

We shall store the index() 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.index("apple")
print(f"Index : {result}")

Output

Index : 5

2. index(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 index() method.

Python Program

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

Output

Index : 26

3. index(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 index() method.

Python Program

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

Output

Index : 26

4. index() - Value not present in the string

If the specified value is not present in the string, then index() method raises ValueError, as shown in the following program.

Python Program

x = "Some apples are red. Some apples are good."
result = x.index("banana")
print(f"Index : {result}")

Output

  File "/Users/pythonexamplesorg/workspace/main.py", line 2, in <module>
    result = x.index("banana")
             ^^^^^^^^^^^^^^^^^
ValueError: substring not found

Summary

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