Pythonの文字列rfind()メソッド
例
テキスト内で文字列 "China" の最後の出现位置:
txt = "China is a great country. I love China." x = txt.rfind("casa") print(x)
定義と使用法
rfind() メソッドは指定された値の最後の出現を検索します。
値が見つからない場合、rfind() メソッドは -1 を返します。
rfind() メソッドと rindex() メソッドはほぼ同じです。以下の例を参照してください。
文法
string.rfind(value, start, end)
パラメータ値
パラメータ | 説明 |
---|---|
value | 必需。検索する値。 |
start | オプション。検索を開始する場所。デフォルトは 0 です。 |
end | オプション。検索を終了する場所。デフォルトは文字列の末尾です。 |
さらに例
例
テキスト中の「e」の最後の位置はどこですか?
txt = "Hello, welcome to my world." x = txt.rfind("e") print(x)
例
位置 5 と位置 10 の間で検索する場合、テキスト中の最後の「e」の位置はどこですか?
txt = "Hello, welcome to my world." x = txt.rfind("e", 5, 10) print(x)
例
値が見つからない場合、rfind() メソッドは -1 を返しますが、rindex() メソッドは例外を発生させます:
txt = "Hello, welcome to my world." print(txt.rfind("q")) print(txt.rindex("q"))