Python String rsplit() Method
Example
Split a string into a list, using comma, followed by a space (, ) as the separator:
txt = "apple, banana, cherry"
x = txt.rsplit(", ")
print(x)
Run example »
Definition and Usage
The rsplit()
method splits a string into a
list, starting from the right.
If no "max" is specified, this method will return the same as the
split()
method.
Note: When max is specified, the list will contain the specified number of elements plus one.
Syntax
string.rsplit(separator, max)
Parameter Values
Parameter | Description |
---|---|
separator | Optional. Specifies the separator to use when splitting the string. Default value is a whitespace |
max | Optional. Specifies how many splits to do. Default value is -1, which is "all occurrences" |
More Examples
Example
Split the string into a list with maximum 2 items:
txt = "apple, banana, cherry"
# setting the max parameter
to 1, will return a list with 2 elements!
x = txt.rsplit(", ", 1)
print(x)
Run example »