0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Pythonで「指定した文字列が対象の文字列に含まれる位置を取得する(find, rfind, index, rindex)」の動作を確認してみた

Posted at

概要

Pythonで「指定した文字列が対象の文字列に含まれる位置を取得する(find, rfind, index, rindex)」の動作を確認してみました。以下のページを参考にしました。

実装

以下のファイルを作成しました。

sample.py
print("dictionary".find("io"))
print("Good School".find("oo"))
print("Goooood".find("oo"))
print("Orange".find("aa"))
print("")
print("Good School".find("oo"))
print("Good School".find("oo", 3, 10))
print("Good School".find("oo", 3))
print("")
print("dictionary".index("io"))
print("Good School".index("oo"))
print("Good School".index("oo", 3))
#print("Orange".index("aa"))
print("")
print("dictionary".rfind("io"))
print("Good School".rfind("oo"))
print("Goooood".rfind("oo"))
print("Orange".rfind("aa"))
print("")
print("Good School".rfind("oo"))
print("Good School".rfind("oo", 0, 6))
print("Good School".rfind("oo", 3))
print("")
print("dictionary".rindex("io"))
print("Good School".rindex("oo"))
print("Good School".rindex("oo", 0, 6))
#print("Orange".rindex("aa"))

以下のコマンドを実行しました。

$ python3 sample.py 
4
1
1
-1

1
8
8

4
1
8

4
8
4
-1

8
1
8

4
8
1

まとめ

何かの役に立てばと。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?