This python function is used to return the index position of the first occurrence of a specified string. It returns ValueError if the specified text not found.
In this section, we discuss how to write Python string index Function with example and the syntax is.
Str_Value.index(Substring, Starting_Position, Ending_Position)
The position of the Function starts from 0, Not 1.
- Substring: Please specify the text you want to search for.
- Starting_Position: This is an optional parameter. If you want to pass the beginning point, then Please specify the value here. If you omit this parameter, it considers Zero as a beginning position.
- Ending_Position: This is an optional parameter. If you want to provide the endpoint, then Please specify an it here. If you omit this parameter, it considers the highest number.
Python index method Example
The following set of examples helps you understand it. The first statement finds the word ‘abc’ inside the Str1 and prints the output
The Python string index function allows us to use starting position. By providing the starting location, we can increase performance. So, let’s use the first position as 12
It also allows us to use Starting and ending values. In the nest line, we are passing the starting and ending locations. The following Method statement begins looking for ‘abc’ from 12 from ends at last.
The last Python statement is returning Value Error. Because the function begins looking from 12 (means the first abc skipped) and ends at 21. As we all know, the second abc is in position 22.