Python rsplit() String Method: What To Know About It?

Claudio Sabato
6 min readSep 21, 2023

--

Trying to understand if you should use Python rsplit in your program or why it is used in code written by someone else? Let’s find out!

The rsplit() string method can be used to split a string into a list of strings using an optional separator. The rsplit() method also allows you to decide the number of splits when splitting a string. This is a very useful method that can be used when manipulating strings in Python.

Let’s learn what rsplit() does and different ways to use it!

What Does rsplit() Do in Python?

Python’s rsplit() method splits a string into a list of strings. The separator to be used when splitting a string and the number of substrings to split the initial string into can be passed as optional parameters.

Below you can see the syntax of the Python rsplit() string method:

string.rsplit(separator=None, maxsplit=- 1)

The return value of the rsplit() method is a list of strings. Both rsplit() parameters are optional.

Let’s see how these two parameters are used:

ParameterOptional?DescriptionseparatorYesThis is the separator used to split the string. The rsplit() method splits a string starting from the right. The default separator is the whitespace character.maxsplitYesThis is the maximum number of splits to be used when splitting a string. When you pass maxsplit to the rsplit() method the result is a list of strings with maxsplit + 1 elements. For example, if maxsplit is 2, rsplit() returns a list of strings with 3 elements. The default is -1 which means unlimited splits.

The table above explains how both rsplit() parameters are used and their default values.

Basic Usage of the rsplit() Method

It’s time to learn how you can use the Python string rsplit() method in your code!

Open the Python shell…

We will start with a simple example and we will make small changes to it to show multiple ways to use it.

Here is the string we will use for these rsplit examples:

>>> objects = "phone, glass, table, chair"

Example 1: Using rsplit() without separator. Split on whitespace

>>> objects.rsplit()
['phone,', 'glass,', 'table,', 'chair']

The rsplit() method has split the string using the whitespace as a separator. That’s why the value of the first element of the list of strings returned is ‘phone,’ and so on for the other elements.

Example 2: Using rsplit() with a separator

>>> objects.rsplit(",")
['phone', ' glass', ' table', ' chair']

This time we have used the comma as a separator and the result is better than the one in the previous example because each element in the list of strings returned by rsplit() doesn’t contain the comma character.

Let’s try to use a different separator:

>>> objects.rsplit(", ")
['phone', 'glass', 'table', 'chair']

The string in the list of strings returned looks a lot better compared to the output before!

Now that we have used the comma followed by a whitespace as a separator we have removed the whitespace that in the previous example was at the beginning of the 2nd, 3rd, and 4th elements.

Example 3: Using rsplit() with a separator and a maxsplit value to limit the number of splits

>>> objects.rsplit(", ", 1)
['phone, glass, table', 'chair']

In this example, we have kept the comma as a separator and we have passed a maxsplit equal to 1. This means that the list of substrings returned by rsplit() contains two elements.

You can see that rsplit() started splitting the string from the end. That’s why the second element is ‘chair’ and the first element contains all the other substrings including the commas between them.

Let’s increase the maxsplit value to make sure it’s clear to you how the list of strings returned changes depending on the maxsplit value.

>>> objects.rsplit(", ", 2)
['phone, glass', 'table', 'chair']

Now that maxsplit is 2, the list of strings returned has 3 elements. The initial string is still split from the end.

Let’s see the result when maxsplit is equal to 3.

>>> objects.rsplit(", ", 3)
['phone', 'glass', 'table', 'chair']

This time all the substrings in the initial string are split correctly because the initial string has 4 substrings and by using a value of 3 for maxsplit we obtain a list of strings with 4 elements.

Make sure this behavior is clear to you and don’t worry if it looks a bit confusing the first time you see how rsplit() works. You just have to get used to it.

What is the Difference Between rsplit() and split() in Python?

Python’s rsplit() and split() methods are very similar. They both split a string into a list of strings with the difference that rsplit() splits a string starting from the end to the beginning of the string while the split() method splits a string starting from the beginning of the string.

Let’s open the Python shell and create a string.

We will see what is the difference between rsplit() and split() when we apply them to this string using the comma as a separator.

>>> objects = "phone, glass, table, chair"
>>> objects.rsplit(",")
['phone', ' glass', ' table', ' chair']
>>> objects.split(",")
['phone', ' glass', ' table', ' chair']

Interestingly the list of strings returned by rsplit() and split() are identical. They both have four elements.

Why?

That’s because…

The behavior of rsplit() and split() is identical if you don’t pass the maxsplit parameter to these methods. The reason behind this is that the main difference between rsplit() and split() is that rsplit() starts splitting a string from the end while split() starts splitting a string from the beginning.

Let’s see how this works in practice.

We will use the previous code and we will also pass a maxsplit value of 2 to both methods:

>>> objects.rsplit(",", 2)
['phone, glass', ' table', ' chair']
>>> objects.split(",", 2)
['phone', ' glass', ' table, chair']

Okay, now the lists of strings returned by the two string methods are different.

First of all, there is one aspect in common. In both method calls the resulting list of strings have 3 elements because the value passed for maxsplit is 2.

You can see that the main difference is the following:

  • rsplit() starts splitting the string from the end (right side). It can be useful when you only need to split the last few substrings in a string.
  • split() starts splitting the string from the beginning (left side).

Can you see the difference in the list of strings returned by the two methods?

Make sure you can see it before continuing through this tutorial.

Using rsplit() Together With Other String Methods

In Python, you can chain multiple string methods. This can be useful to make your code more concise and powerful.

For example, let’s have a look at what code you would write if you want to convert your string to uppercase before splitting it using the rsplit() method.

>>> objects.upper().rsplit(", ")
['PHONE', 'GLASS', 'TABLE', 'CHAIR']

Interestingly, with a single line of code we have created a list of strings but this time the strings are upper case.

The way we have done this is by chaining two string method calls: upper() and rsplit().

First, we applied the upper() method to the original string and then chained the rsplit() method to it in order to split the string in the same way we have seen previously.

You can see how this approach can be very useful in your Python program, especially considering that you have plenty of other string methods to play with.

Conclusion

You should now have a full understanding of how rsplit() works in Python when handling strings.

Choosing between rsplit() and split() depends on your specific use case and after the examples we went through you know how to choose the right one for you.

Related article: Now that you have learned how to use the rsplit() method it’s time to learn another very useful string method that will improve your string formatting knowledge, the Python string join() method!

--

--

Claudio Sabato
Claudio Sabato

Written by Claudio Sabato

Claudio Sabato is an IT expert with over 15 years of professional experience in Python/Bash programming, Linux Systems Administration and IT Systems Design.

No responses yet