Skip to main content

Python String index() Function

The String index() method searches for the first occurrence of the given string and returns its index. If specified string is not found, it raises a ValueError exception.

The optional arguments start and end can be used to limit the search to a particular substring of the string.

note

If you want to get the last occurrence of the specified string, then you have to use the rindex() method.

Syntax

my_string.index(sub, start, end)

index() Parameters

Python String index() function parameters:

ParameterConditionDescription
subRequiredAny string you want to search for
startOptionalAn index specifying where to start the search. Default value is 0.
endOptionalAn index specifying where to stop the search. Default value is the end of the string.

index() Return Value

Python String index() function returns an integer value corresponding to the first occurrence of the substring, if the substring exists inside the string.

danger

If a substring does not exist inside the string, it raises a ValueError exception.

Examples

Example 1: Get index of given String in a String

If the given string is contained in the string, then index() method returns the index of the first occurrence:

my_str = 'Tom is a Developer on tutorialreference.com Development Team'
result = my_str.index('Dev')
print(result) # Output: 9

output

9

If the given string is not found in the string, then index() method raises ValueError exception.

my_str = 'Tom is a Developer on tutorialreference.com Development Team'
result = my_str.index('Ryan') # Raises ValueError: substring not found

output

Traceback (most recent call last):
File "main.py", line 2, in <module>
result = my_str.index('Ryan')
ValueError: substring not found

Example 2: Get index of given String in a String with start and end indexes

If you want to limit the search to a substring of the given string, you can specify the start parameter and/or the end parameter.

my_str = 'Tom is a Developer on tutorialreference.com Development Team'
sub = "Dev"

# index()
result = my_str.index(sub)
print(result) # Output: 9

# index() after 4th index
result = my_str.index(sub, 4)
print(result) # Output: 9

# index() between 4th and 6th index
result = my_str.index(sub, 4, 6)
print(result) # Raises ValueError: substring not found

output

9
9
Traceback (most recent call last):
File "main.py", line 13, in <module>
result = my_str.index(sub, 4, 6)
ValueError: substring not found

Example 3: Handling ValueError of index() method

You can handle the ValueError exception using a try-except block.

my_str = "Python is powerful."
sub = "Java"
try:
index = my_str.index(sub)
print("The index where the substring is found:", index)
except ValueError:
print("The substring does not exist in the text.")

output

The substring does not exist in the text.

index() method vs find() method

The index() method is identical to the find() method.

The only difference is that the find() method returns -1 (instead of raising a ValueError exception), if the substring is not found.

An example to compare find() and index():

my_str = 'Tom is a Developer at ABC'
result = my_str.find('Manager')
print(result) # Output: -1

my_str = 'Tom is a Developer at ABC'
result = my_str.index('Manager') # Triggers ValueError: substring not found

output

-1
Traceback (most recent call last):
File "main.py", line 6, in <module>
result = my_str.index('Manager')
ValueError: substring not found