Python String rsplit() Method
Example
Split the string into a list using a comma followed by a space as the delimiter:
txt = "apple, banana, cherry" x = txt.rsplit(', ') print(x)
Definition and Usage
The rsplit() method splits the string into a list starting from the right.
If "max" is not specified, this method will return the same result as the split() method.
Note:If max is specified, the list will contain one more element than the specified number.
Syntax
string.rsplit(separator, max)
Parameter Value
Parameter | Description |
---|---|
separator | Optional. Specify the delimiter to be used when splitting the string. The default value is whitespace. |
max | Optional. Specify the number of splits to be executed. The default value is -1, which means "all occurrences". |
More Examples
Example
Split the string into a list of up to 2 items:
txt = "apple, banana, cherry" Set the max parameter to 1, and the list containing 2 elements will be returned! x = txt.rsplit(', ', 1) print(x)