String index

Return the index of a substring (if found). Returns exception if not found.

It’s case sensitive.

Syntax

The format is:

str.index(string)

or

str.index(string,start,end)

Parameters

Parameter
string
The string to search.
start
Index to start searching (optional).
end
Ending index (optional).

Example

str = "Tip of the day: "
print( str.index("Tip") )

Result:


0

Example #2

str = "Tip of the day: "
print( str.index("Tip",5) ) # throws exception

Result:


Traceback (most recent call last):
File "tmp.py", line 2, in
print( str.index("Tip") )
ValueError: substring not found