Python String rindex() Method


Contents

Description

The rindex() method in Python String returns the last or highest index where the substring inside the string in case substring is found is found, else it raises an exception when no such index exists, and this optionally restricts the search to a specific string[beg:end].

Syntax

Here is the syntax for rindex() method:

str.rindex(str, beg=0 end=len(string))

Parameters

  • str − This indicates the string which is to be searched.
  • beg − This is the starting index and by default it is 0
  • len − This is the end index, and it is equal to the length of the existing string by default.

Return Value

This method is responsible to return the last index when found otherwise it raises an exception when str is not found.

Example

The following example shows the usage of rindex() method.

#!/usr/bin/python

str1 = "this is string example....wow!!!";
str2 = "is";

print str1.rindex(str2)
print str1.index(str2)

When the above program is run, it produces following result:

5
2

Here at Intellinuts, we have created a complete Python tutorial for Beginners to get started in Python.



Navigation
Tutorial
Tutorial
Tutorial