Python String index() Method
Python index() method is same as the find() method except it returns error on failure. This method returns index of first occurred substring and an error if there is no match found.
If found it returns an index of the substring, otherwise an error ValueError.
Let's see some examples to understand the index() method.
Python String index() Method Example 1
Python String index() Method Example 2
An error is thrown if the substring is not found.
ValueError: substring not found
Python String index() Method Example 3
We can also pass start and end index as parameters to make process more customized.
p is present at : 20 index