Latest web development tutorials

Python3 rfind()方法

Python3 字符串 Python3字符串


描述

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

語法

rfind()方法語法:

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

參數

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

返回值

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

實例

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

#!/usr/bin/python3

str1 = "this is really a string example....wow!!!"
str2 = "is"

print (str1.rfind(str2))

print (str1.rfind(str2, 0, 10))
print (str1.rfind(str2, 10, 0))

print (str1.find(str2))
print (str1.find(str2, 0, 10))
print (str1.find(str2, 10, 0))

以上實例輸出結果如下:

5
5
-1
2
2
-1

Python3 字符串 Python3字符串