The rsplit method
The rsplit
method returns a list of a string split by the last match of the substring and the separator specified in the parameter. In the second optional parameter, we specify how many times to split the string. By default, you can split the string an unlimited number of times.
Syntax
string.rsplit(separator, [number of divisions])
Example
Let's apply the rsplit
method to the following string and split it once:
txt = 'ab_ac_dea'
print(txt.rsplit('_', 1))
Result of code execution:
['ab_ac', 'dea']
Example
Now let's apply the rsplit
method without specifying the number of divisions:
txt = 'ab_ac_dea'
print(txt.rsplit('_'))
Result of code execution:
['ab', 'ac', 'dea']
See also
-
method
split
,
which divides a string by the substring on the left -
method
rpartition
,
which divides a string by the last match of a substring -
method
rsplit
,
which divides a string by the substring on the right -
method
partition
,
which divides a string by the first match of a substring -
method
join
,
which returns a string from a list of strings