Pour vérifier si un string contient un autre string avec python le plus rapide est d'utiliser 'in' comme dans cet exemple (Remarque: attention sensible à la casse):
>>> s = 'Hello World !'>>> 'Wo' in sTrue>>> 'wo' in sFalse
Pour vérifier si un string contient un autre string avec python et obtenir en plus une information sur la position de celui-ci, il existe la méthode find (Built-in Types) associée aux "string".
>>> index = index = s.find('Wo')>>> if index == -1:... print 'Not Found'... else:... print "Found at index", index...Found at index 6
Recherches associées
| Liens | Site |
|---|---|
| How do I check if a given Python string is a substring of another one? [duplicate] | stackoverflow |
| how to find whether a string is contained in another string | stackoverflow |
| Built-in Types | Site Web Python |
