Python3 字符串 Python3 字符串


描述

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

語法

rfind()方法語法:

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

參數(shù)

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

返回值

返回字符串最后一次出現(xiàn)的位置,如果沒有匹配項則返回-1。

實例

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

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

以上實例輸出結(jié)果如下:

5
5
-1
2
2
-1

Python3 字符串 Python3 字符串