Python rindex() Method

Python 字符串Python Strings


Description

Python rindex() returns the last position where the substring str appears in the string. If there is no matching string, an exception will be raised. You can specify the optional parameter [beg:end] to set the search range.

Syntax

rindex() method syntax:

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

Parameters

  • str -- The string to search for
  • beg -- The starting search position, defaults to 0
  • end -- The ending search position, defaults to the length of the string.

Return Value

Returns the last position where the substring str appears in the string. If there is no matching string, an exception will be raised.

Examples

The following example demonstrates the usage of 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 as follows:

5
2

Python 字符串Python Strings

Other Extensions