Python – La méthode String partition()
La méthode partition() recherche 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.partition(value)
Paramètres:
La méthode partition() 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.partition("Python") print(res)
Sortie:
('I love ', 'Python', ' as a programming language')
Exemple 2:
str = "I love Python as a programming language" res = str.partition("Java") print(res)
Sortie:
('I love Python as a programming language', '', '')