Python String rsplit() Function
The String rsplit()
method splits the string on a specified delimiter and returns the list of substrings.
If a delimiter is not specified, the string is split on whitespace.
By default, the rsplit()
method will make all possible splits (no limit on the number of splits). However, if you specify maxsplit
, then only the given number of splits will be made, starting from the end of the string.
Syntax
my_string.rsplit(delimiter, maxsplit)
rsplit() Parameters
Python String rsplit()
function parameters:
Parameter | Condition | Description |
---|---|---|
delimiter | Optional | Any character to split the sting with. Default value is whitespace. |
maxsplit | Optional | A number specifying how many splits to make. Default value is -1 (i.e. no limit on splits) |
rsplit() Return Value
Python String rsplit()
function returns a list of substrings obtained by splitting the original string.
Examples
Example 1: Split a String on Whitespace with rsplit()
When delimiter
parameter is not specified, the string is split on whitespace.
my_str = 'This is Tutorial Reference'
result = my_str.rsplit()
print(result) # Output: ['This', 'is', 'Tutorial', 'Reference']
output
['This', 'is', 'Tutorial', 'Reference']
Note that the rsplit()
method automatically combines consecutive whitespace into single delimiter and splits the string
my_str = 'This is Tutorial Reference'
result = my_str.rsplit()
print(result) # Output: ['This', 'is', 'Tutorial', 'Reference']
output
['This', 'is', 'Tutorial', 'Reference']
Moreover, newline \n
, tab \t
and carriage return \r
are also considered as whitespace characters.
my_str = 'This\n\ris\tTutorial \n Reference'
result = my_str.rsplit()
print(result) # Output: ['This', 'is', 'Tutorial', 'Reference']
output
['This', 'is', 'Tutorial', 'Reference']
Example 2: Split a String on Custom Delimiter with rsplit()
You can split a string by specifying a custom delimiter
.
my_str = 'Tom, Anna, David, Ryan'
result = my_str.rsplit(',')
print(result) # Output: ['Tom', ' Anna', ' David', ' Ryan']
output
['Tom', ' Anna', ' David', ' Ryan']
my_str = 'First Line\nSecond Line\nThird Line'
result = my_str.rsplit('\n')
print(result) # Output: ['First Line', 'Second Line', 'Third Line']
output
['First Line', 'Second Line', 'Third Line']
Note that a delimiter
can contain multiple characters.
my_str = 'This is Tutorial Reference'
result = my_str.rsplit(' is ')
print(result) # Output: ['This', 'Tutorial Reference']
output
['This', 'Tutorial Reference']
Example 3: Split a String for a Certain Number of Times with rsplit()
You can specify maxsplit
parameter: it means that only the given number of splits will be made. The resulting list will have the specified number of elements plus one.
For example, split using whitespace as delimiter
(None in order to use default value) and maxsplit
to 1:
my_str = 'This is Tutorial Reference'
result = my_str.rsplit(None, 1)
print(result) # Output: ['This is Tutorial', 'Reference']
output
['This is Tutorial', 'Reference']
But, we can change the maxsplit
to increase the number of splits:
my_str = 'This is Tutorial Reference'
result = my_str.rsplit(None, 2)
print(result) # Output: ['This is', 'Tutorial', 'Reference']
output
['This is', 'Tutorial', 'Reference']
Note that if maxsplit
is not specified or it is -1
, then rsplit()
method will make all possible splits (i.e. no limit on the number of splits).
my_str = 'This is Tutorial Reference'
result1 = my_str.rsplit(None, -1) # maxsplit = -1
result2 = my_str.rsplit(None) # maxsplit not specified
print(result1) # Output: ['This', 'is', 'Tutorial Reference']
print(result1) # Output: ['This', 'is', 'Tutorial Reference']
output
['This', 'is', 'Tutorial', 'Reference']
['This', 'is', 'Tutorial', 'Reference']
Example 4: Split a String with multiple Delimiters with rsplit()
The rsplit()
method does not allow for multiple delimiters. Instead, you can use regular expression with re.split()
method.
For example, let's use regular expression to split with comma ,
, semicolon ;
and colon :
:
import re # import regular expression package
my_str = 'Tom,Ryan;Anna:David'
result = re.rsplit('[,;:]',my_str)
print(result) # Output: ['Tom', 'Ryan', 'Anna', 'David']
output
['Tom', 'Ryan', 'Anna', 'David']
rsplit() method vs split() method
- If
maxsplit
is specified, thersplit()
method counts splits from the end of the string, whilesplit()
method counts them from the beginning of the string. - If
maxsplit
is not specified, they both behave exactly the same.
# split()
my_str = 'This is Tutorial Reference'
result = my_str.split(None, 1)
print(result) # Output: ['This', 'is Tutorial Reference']
# rsplit()
my_str = 'This is Tutorial Reference'
result = my_str.rsplit(None, 1)
print(result) # Output: ['This is Tutorial', 'Reference']
output
['This', 'is Tutorial Reference']
['This is Tutorial', 'Reference']
Unpacking, Indexing and Slicing the result of rsplit()
The rsplit()
method returns a list of strings. So, you can perform any operation on lists, like multiple assignment (unpacking), indexing, slicing, etc.
Some examples:
# multiple assignment
my_str = 'Tom,David,Anna'
x,y,z = my_str.rsplit(',')
print(x) # Output: Tom
print(y) # Output: David
print(z) # Output: Anna
# indexing
my_str = 'Tom,David,Anna,Ryan'
result = my_str.rsplit(',')[2]
print(result) # Output: Anna
# slicing
my_str = 'Tom,David,Anna,Ryan'
result = my_str.rsplit(',')[1:3]
print(result) # Output: ['David', 'Anna']
output
Tom
David
Anna
Anna
['David', 'Anna']