Python 文字列に部分文字列が存在するかどうかを判定する

Python3 サンプル

文字列が与えられたとき、その文字列内に指定された部分文字列が存在するかどうかを判定します。

サンプル

def check(string, sub_str): if (string.find(sub_str) == -1): print("存在しません!") else: print("存在します!") string = "www.example.com" sub_str ="サンプル" check(string, sub_str)

上記のコードを実行すると、出力結果は次のようになります:

存在する!

Python3 サンプル

その他の拡張