Méthode rfind() de Python
Chaînes de caractères en Python
Description
Python rfind() renvoie la position de la dernière occurrence de la chaîne, ou -1 si aucune correspondance n'est trouvée.
Syntaxe
Syntaxe de la méthode rfind() :
str.rfind(str, beg=0, end=len(string))
Parameters
- str -- La chaîne à rechercher
- beg -- La position de début de recherche, par défaut 0
- end -- La position de fin de recherche, par défaut la longueur de la chaîne.
Valeur de retour
Renvoie la position de la dernière occurrence de la chaîne, ou -1 si aucune correspondance n'est trouvée.
Exemple
L'exemple suivant montre comment utiliser la fonction rfind() :
Exemple
#!/usr/bin/python
str = "this is really a string example....wow!!!";
substr = "is";
print str.rfind(substr);
print str.rfind(substr, 0, 10);
print str.rfind(substr, 10, 0);
print str.find(substr);
print str.find(substr, 0, 10);
print str.find(substr, 10, 0);
str = "this is really a string example....wow!!!";
substr = "is";
print str.rfind(substr);
print str.rfind(substr, 0, 10);
print str.rfind(substr, 10, 0);
print str.find(substr);
print str.find(substr, 0, 10);
print str.find(substr, 10, 0);
La sortie de l'exemple ci-dessus est la suivante :
5 5 -1 2 2 -1
Autres extensions