Python文字列rfind()メソッド
例
文字列「casa」の最後の出現はテキストのどこにありますか?:
txt = "Mi
casa, su casa."
x = txt.rfind("casa")
print(x)
定義と使用法
このrfind()
メソッドは、指定された値の最後の出現を検索します。
rfind()
値が見つからない場合、メソッドは-1を返します。
rfind()
方法はほとんど同じ
rindex()
です。以下の例を参照してください。
構文
string.rfind(value, start, end)
パラメータ値
Parameter | Description |
---|---|
value | Required. The value to search for |
start | Optional. Where to start the search. Default is 0 |
end | Optional. Where to end the search. Default is to the end of the string |
その他の例
例
テキストのどこに文字「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"))