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.
Syntax
my_string.index(sub, start, end)index() Parameters
Python String index() method parameters:
| Parameter | Condition | Description |
|---|---|---|
sub |
Required | Any string you want to search for |
start |
Optional | An index specifying where to start the search. Default value is 0. |
end |
Optional | An index specifying where to stop the search. Default value is the end of the string. |
index() Return Value
Python String index() method returns an integer value corresponding to the first occurrence of the substring, if the substring exists inside the string.
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: 9output
9If 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 foundoutput
Traceback (most recent call last):
File "main.py", line 2, in <module>
result = my_str.index('Ryan')
ValueError: substring not foundExample 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"
result = my_str.index(sub)
print(result) # Output: 9
result = my_str.index(sub, 4)
print(result) # Output: 9
result = my_str.index(sub, 4, 6)
print(result) # Raises ValueError: substring not foundoutput
9
9
Traceback (most recent call last):
File "main.py", line 13, in <module>
result = my_str.index(sub, 4, 6)
ValueError: substring not foundExample 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 foundoutput
-1
Traceback (most recent call last):
File "main.py", line 6, in <module>
result = my_str.index('Manager')
ValueError: substring not found