Chapter 18

  5.3.9 文字列の検索

小栁 斉
小栁 斉
2024.09.22に更新
  • str.find():部分文字列が含まれる場合、その最小のインデックスを返す。見つからなかった場合-1を返す。
  • str.rfind():部分文字列が含まれる場合、その最大のインデックスを返す。見つからなかった場合-1を返す。
サンプルコード:sample_274.py
x = "abcabcabc"
print(x.find("b"))
実行結果
1
サンプルコード:sample_275.py
x = "abcabcabc"
print(x.find("b", 3, 9))
実行結果
4
サンプルコード:sample_276.py
x = "abcabcabc"
print(x.find("z"))
実行結果
-1
サンプルコード:sample_277.py
x = "abcabcabc"
print(x.rfind("b"))
実行結果
7
サンプルコード:sample_278.py
x = "abcabcabc"
print(x.rfind("b", 0, 6))
実行結果
4
サンプルコード:sample_279.py
x = "abcabcabc"
print(x.rfind("z"))
実行結果
-1