Python String rsplit() Method
The rsplit()
method is same as split method that splits a string from the specified separator and returns a list object with string elements.
The default separator is any whitespace character such as space, \t
, \n
, etc.
The only difference between the split()
and rsplit()
is when the maxsplit parameter is specified. If the maxsplit parameter is specified, then the rsplit()
method starts splitting a string from the right side (from the last character),
whereas the split()
method starts splitting from the left side (from the first character).
Syntax:
str.rsplit(separator, maxsplit)
Parameters:
- separator: (optional) The delimiter string. The default separator is any whitespace character such as space,
\t
,\n
, etc. - maxsplit: (optional) Defines the maximum number of splits that can be done. Thus, the list can contain at most
maxsplit + 1
elements. The default maxsplit is -1 that means unlimited splits.
Return Value:
Returns a list object with string elements.
The following example demonstrates the simple use of the rsplit()
method, which is same as the split()
method.
mystr = 'Hello World'
print(mystr.rsplit())
print('Hello World'.rsplit())
print('Hello\tWorld'.rsplit())
print('Hello\nWorld'.rsplit())
print('Hello\u2028World'.rsplit())
['Hello', 'World']
['Hello', 'World']
['Hello', 'World']
['Hello', 'World']
['Hello', 'World']
In the above example, all the string splits at the default whitespace characters such as ' ', ' ', '\t', and '\n' and returns a list ['Hello', 'World']
.
Even it splits at the Unicode char of line separator '\u2028'.
The following examples specifies the separator.
langs = 'C,Python,R,Java,SQL,Hadoop'
print(langs.rsplit(','))
fruits = 'apples$banana$mango$fig$pear'
print(fruits.rsplit('$'))
['C', 'Python', 'R', 'Java', 'SQL', 'Hadoop']
['apples', 'banana', 'mango', 'fig','pear']
In the above example, the langs.rsplit(',')
specifies a comma ,
as a separator and fruits.rsplit('$')
specifies the $
symbol as a separator.
So, the rsplit()
method will rsplit a string at each separator and include each part of a string in a list.
If the specified seperator does not exist, then it returns a list with the whole string as an element.
langs = 'C,Python,R,Java,SQL,Hadoop'
print(langs.rsplit('@'))
['C,Python,R,Java,SQL,Hadoop']
rsplit()
method will raise the ValueError
if a separator is an empty string ''
.
The following example limits the rsplit by specifying the maxsplit
parameter.
langs = 'C,Python,R,Java,SQL,Hadoop'
print(langs.rsplit(',', 3))
fruits = 'apples$banana$mango$fig$pear'
print(fruits.rsplit('$', 2))
['C,Python,R', 'Java', 'SQL', 'Hadoop']
['apples$banana$mango', 'fig', 'pear']
In the above example, the langs.rsplit(',', 3)
specifies 3 as maxsplit argument, so it will split langs
string 3 times and so a list object includes four element.
It starts splitting from right side, so the first element is the remaining string. (This is where it is different from the split()
method.)
In the same way, the fruits.rsplit('$', 2)
will be rsplit maximum of two times, and the returned list will include three elements.