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

その他の拡張