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)

Run Instance

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

.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 max parameter to 1, it will return a list containing 2 elements!
x = txt.rsplit(', ', 1)
print(x)

Run Instance