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 文字列

その他の拡張