The index() method in Python is almost the same as the find() method, the only difference is that the find() method returns -1 if that value is not found.

Python String Index Example

Python String index() is an inbuilt function that returns the index of a substring inside the string if found. If the substring is not found, it raises an exception.  The index() method finds the first occurrence of the specified value. The index() method raises the exception if the value is not found.

See the following syntax.

string.index(value, start, end)

The value parameter is required which is the value we search for.

The start parameter is optional; it is Where to start the search. The default is 0.

The end parameter is optional; it is where to end the search. The default is to the end of the string.

#python #python string index

Python String Index: How to Get String Index in Python
1.35 GEEK