Python 3 reference · String Methods

Python String rsplit() Method

Split the string into a list from the right. With maxsplit, only the last few splits are made.

Python 3 Since 3.0

Syntax

str.rsplit(sep=None, maxsplit=-1)

Parameters

ParameterDescription
sep optional The separator; default splits on runs of whitespace. Default: None
maxsplit optional Maximum number of splits, counted from the right. Default: -1

Return value

A list of strings.

Examples

rsplit vs split with maxsplitTry it yourself
print("a,b,c,d".rsplit(",", 1))
print("a,b,c,d".split(",", 1))

Output

['a,b,c', 'd']
['a', 'b,c,d']
Split off the last wordTry it yourself
full_name = "Mary Ann Smith"
first, last = full_name.rsplit(" ", 1)
print(first, "|", last)

Output

Mary Ann | Smith

Version history

VersionChange
Python 3.0Added