Python - Vérifier si une chaîne contient une autre chaîne?

Python - Vérifiez si une chaîne contient une autre chaîne?

En Python, nous pouvons utiliser l'opérateurin oustr.find() pour vérifier si une chaîne contient une autre chaîne.

1. en opérateur

name = "example is learning python 123"

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

Sortie

found python!

2. str.find()

name = "example is learning python 123"

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

Sortie

found python!

Pour une recherche insensible à la casse, essayez de convertir la chaîne en majuscules ou en minuscules avant de la rechercher.

name = "example is learning python 123"

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

Sortie

found python!