Python3 rindex()メソッド

Python3 字符串Python3 文字列


説明

rindex() は、部分文字列 str が文字列内で最後に出現する位置を返します。一致する文字列がない場合は例外が発生します。オプションのパラメータ [beg:end] を指定して、検索範囲を設定できます。

構文

rindex()メソッドの構文:

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

引数

  • str -- 検索する文字列
  • beg -- 検索を開始する位置、デフォルトは0
  • end -- 検索を終了する位置、デフォルトは文字列の長さ。

戻り値

部分文字列 str が文字列内で最後に出現する位置を返します。一致する文字列がない場合は例外が発生します。

例

以下の例は、rindex()関数の使用方法を示しています:

#!/usr/bin/python3
str1 = "this is really a string example....wow!!!"
str2 = "is"

print (str1.rindex(str2))
print (str1.rindex(str2,10))

上記の例の出力結果は次のとおりです:

5
Traceback (most recent call last):
  File "test.py", line 6, in <module>
    print (str1.rindex(str2,10))
ValueError: substring not found

Python3 字符串Python3 文字列

その他の拡張