index() String Method in Python with Examples

index() method return the lowest index in string where substring sub is found, such that sub is contained within S[start:end]. Optional arguments start and end are interpreted as in slice notation.

index() method raises ValueError when the substring is not found.

Example: index() String Method

message = 'welcome to python'

index = message.index('co')

print('substring co starts from index %d.' %(index))


substring co starts from index 3.

Related String Methods