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
| Parameter | Description |
|---|---|
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
print("a,b,c,d".rsplit(",", 1))
print("a,b,c,d".split(",", 1))
Output
['a,b,c', 'd'] ['a', 'b,c,d']
full_name = "Mary Ann Smith"
first, last = full_name.rsplit(" ", 1)
print(first, "|", last)
Output
Mary Ann | Smith
Version history
| Version | Change |
|---|---|
| Python 3.0 | Added |
Related
str.split()· Split the string into a list of substrings at a separator. With no separator it splits on any whitespace.str.rpartition()· Split the string at the last occurrence of a separator into a 3-tuple: before, separator and after.str.join()· Join the strings of an iterable into one string, with this string as the separator between them.- Python Strings · tutorial