Python - 文字列に別の文字列が含まれているかどうかを確認しますか?

Python –文字列に別の文字列が含まれているかどうかを確認しますか?

Pythonでは、in演算子またはstr.find()を使用して、文字列に別の文字列が含まれているかどうかを確認できます。

1. オペレーター内

name = "example is learning python 123"

if "python" in name:
    print("found python!")
else:
    print("nothing")

出力

found python!

2. str.find()

name = "example is learning python 123"

if name.find("python") != -1:
    print("found python!")
else:
    print("nothing")

出力

found python!

大文字と小文字を区別しない検索の場合、検索する前に文字列をすべて大文字または小文字に変換してください。

name = "example is learning python 123"

if name.upper().find("PYTHON") != -1:
    print("found python!")
else:
    print("nothing")

出力

found python!