Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python rindex() method

Python rindex() method

Python Strings Python Strings


Description

Python rindex() returns the last position at which the substring str occurs in a string. If no matching string is found, an exception is raised. You can specify the optional parameters [beg:end] to set the search range.

Syntax

Syntax of the rindex() method:



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

Parameters

Return Value

Returns the position of the last occurrence of the substring str in a string, and raises an exception if no match is found.

Example

The following example shows how to use the rindex() function:



#!/usr/bin/python



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

str2 = "is";



print str1.rindex(str2);

print str1.index(str2);

The output of the above example is:



5

2


Python Strings Python Strings