rindex() function in python by R4R Team

- rindex() is python in-build function which are used to find the index of the substring in the given string.
- rindex() is not the applicable for list data type.

Syntax-

str.rindex(str1,beg,end)

- here str is given string.
- and str1 is the substring whose index is to be find
- beg and end are the starting and ending point of given string where you want to search.

Difference between index() and rindex() in python-

Basically, both function are used to search the location of the substring in given string but
- index() start searching from the left side of the string and also applicable for list.
- and rindex() start searching from the right side of the string and not applicable for list.

Example

string is s="This is is string"
s.index("is") will give 5
s.rindex("is") will give 8


program-

s="this is is string"
print(s.rindex("is"))

s="Hello program"
print(s.rindex("o"))

s="mytext"
print(s.rindex("m"))

s="mytext"
print(s.rindex("t"))


s="mytext"
print(s.rindex("t",0,3))


output-

8
8
0
5
2


-In this program, we search the location of substring in given string by using s.rindex(str).
- In last line, we use s,rindex("t",0,3) it means that we find the 't' in only in range 0 to 3 so it will give index 2 value.
- if it not find the searching element in given string then it will give an error like ValueError: substring not found




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!