Latest web development tutorials

Python rfind()方法

Python 字符串 Python字符串


描述

Python rfind() 返回字符串最後一次出現的位置,如果沒有匹配項則返回-1。

語法

rfind()方法語法:

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

參數

  • str -- 查找的字符串
  • beg -- 開始查找的位置,默認為0
  • end -- 結束查找位置,默認為字符串的長度。

返回值

返回字符串最後一次出現的位置,如果沒有匹配項則返回-1。

實例

以下實例展示了rfind()函數的使用方法:

#!/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);

以上實例輸出結果如下:

5
5
-1
2
2
-1

Python 字符串 Python字符串