English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
La méthode rsplit() coupe la chaîne de caractères à partir de la droite à l'endroit spécifié par le séparateur, et retourne une liste de chaînes.
La syntaxe de rsplit() est :
str.rsplit([separator, maxsplit])
Le méthode rsplit() peut accepter au plus2Un paramètre :
separator(Optionnel)-C'est un séparateur. L'objectif de la méthode est : couper la chaîne de caractères à partir de la droite à l'endroit spécifié par le séparateur.
Si separator n'est pas spécifié, tout espace (espace, retour chariot, etc.) est un séparateur.
maxsplit(Optionnel)- maxsplit définit le nombre maximum de coupes.
La valeur par défaut est maxsplit-1, indicating that the split count is unlimited.
The rsplit() method splits the string into a list starting from the right.
If 'maxsplit' is not specified, this method will return the same result as the split() method.
Note: If maxsplit is specified, the list will contain one more element than the specified number.
text = 'Love thy neighbor' # Split at whitespace print(text.rsplit()) grocery = 'Milk, Chicken, Bread' # Split at ',' print(grocery.rsplit(', ')) # Split at ':' print(grocery.rsplit(':'))
When running the program, the output is:
['Love', 'thy', 'neighbor'] ['Milk', 'Chicken', 'Bread'] ['Milk, Chicken, Bread']
If maxsplit is not specified, the behavior of rsplit() is similar to split().
grocery = 'Milk, Chicken, Bread, Butter' # maxsplit: 2 print(grocery.rsplit(', ')) 2)) # maxsplit: 1 print(grocery.rsplit(', ')) 1)) # maxsplit: 5 print(grocery.rsplit(', ')) 5)) # maxsplit: 0 print(grocery.rsplit(', ', 0))
When running the program, the output is:
['Milk, Chicken', 'Bread', 'Butter'] ['Milk, Chicken, Bread', 'Butter'] ['Milk', 'Chicken', 'Bread', 'Butter'] ['Milk, Chicken, Bread, Butter']
If maxsplit is specified, the list will contain at most maxsplit+1Project.