Str.index will return the lowest index in a string where substring sub is found and raise values error if substring not found in given string.

Syntax of Str.index:

index(sub, start, end)

sub=an element or object which index is to be determined.

start=starting index (must be an integer)

end=ending index(must be an integer)

a=’weather’

a.index(‘er’)

Output:5

er is present at 5 position in given string. It returns lowest index of object.

how to use str.index
Str.index

The Syntax of Str.rindex is same but it will return highest index in string where substring found as shown below

difference between str.index & str.rindex
Str.rindex

 

By SC

One thought on “Str.index & Str.rindex”

Leave a Reply

Your email address will not be published. Required fields are marked *