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:

  1. separator: (optional) The delimiter string. The default separator is any whitespace character such as space, \t, \n, etc.
  2. 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.

Example: Split a String with Default Whitespace Chars
mystr = 'Hello World'
print(mystr.rsplit())

print('Hello     World'.rsplit())
print('Hello\tWorld'.rsplit())
print('Hello\nWorld'.rsplit())
print('Hello\u2028World'.rsplit())
Output
['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.

Example: Split using Separator
langs = 'C,Python,R,Java,SQL,Hadoop'
print(langs.rsplit(','))

fruits = 'apples$banana$mango$fig$pear'
print(fruits.rsplit('$'))
Output
['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.

Example: Split using Separator
langs = 'C,Python,R,Java,SQL,Hadoop'
print(langs.rsplit('@'))
Output
['C,Python,R,Java,SQL,Hadoop']
Note:
The rsplit() method will raise the ValueError if a separator is an empty string ''.

The following example limits the rsplit by specifying the maxsplit parameter.

Example: Split using Separator
langs = 'C,Python,R,Java,SQL,Hadoop'
print(langs.rsplit(',', 3))

fruits = 'apples$banana$mango$fig$pear'
print(fruits.rsplit('$', 2))
Output
['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.

Want to check how much you know Python?