Python – La méthode String rpartition()
La méthode rpartition() recherche la dernière occurrence d’une chaîne spécifiée et divise la chaîne en un tuple contenant trois éléments. Le premier élément contient la partie avant la chaîne spécifiée. Le deuxième élément contient la chaîne spécifiée. Le troisième élément contient la partie après la chaîne.
Syntaxe:
string.rpartition(value)
Paramètres:
La méthode rpartition() prend un seul paramètre:
- valeur(Obligatoire) : La chaîne à trouver
Exemple 1:
Recherchez le mot « Python » et renvoyez un tuple avec trois éléments:
str = "I love Python as a programming language" res = str.rpartition("Python") print(res)
Sortie:
('I love ', 'Python', ' as a programming language')
Exemple 2:
str = "I love Python as a programming language" res = str.rpartition("Java") print(res)
Sortie:
('', '', 'I love Python as a programming language')