Python String index() method

The string.index() method in Python finds the lower index of the first occurrence of the specified value or raise a ValueError if the substring is not found.

Syntax

Parameter

sub: This parameter represents a substring which is to be searched in the given string.

start( Optional): This parameter signifies the substring from where the search is to start and its default value is 0.

end: This parameter signifies the substring from where to end the search and its default value is the end of the string

Return

This method returns the lowest index in the string if the substring is found else it raises a ValueError exception if the specified substring doesn’t exist inside the string.

Example 1

Output

Example 2

Output

Pin It on Pinterest

Share This