Python rfind() Method

Python 字符串Python Strings


Description

Python rfind() returns the last occurrence position of the string, or -1 if no match is found.

Syntax

Syntax of rfind() method:

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

Parameters

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

Return Value

Returns the last occurrence position of the string, or -1 if no match is found.

Example

The following example demonstrates how to use the rfind() function:

Example

#!/usr/bin/python

str = "this is really a string example....wow!!!";
substr = "is";

print str.rfind(substr);
print str.rfind(substr, 0, 10);
print str.rfind(substr, 10, 0);

print str.find(substr);
print str.find(substr, 0, 10);
print str.find(substr, 10, 0);

The output of the above example is as follows:

5
5
-1
2
2
-1

Python 字符串Python Strings

Other Extensions