Python rsplit() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

rsplit() splits a string just like split(), but it starts from the right. It returns a list of strings, and the separator is not kept.

Without maxsplit the result is exactly the same as split(). The difference appears when you limit the cuts: rsplit() makes them at the last separators, so "a-b-c".rsplit("-", 1) is ['a-b', 'c']. That is handy for file extensions and for the last part of an address.

text.rsplit() is a method of the built-in str type: call it on a string with dot notation. No import is needed, and it never changes the original string.

Syntax

Pass a separator and an optional limit. With no arguments it splits on whitespace.

python
parts = text.rsplit()
parts = text.rsplit(sep)
parts = text.rsplit(sep, maxsplit)
CallWhat it does
"a b c".rsplit()Returns ['a', 'b', 'c']. With no limit it works like split().
"2024-05-01".rsplit("-", 1)Returns ['2024-05', '01']. Only the last hyphen is used for a cut.
"one two three".rsplit(None, 1)Returns ['one two', 'three']. None means whitespace, and the single cut is made at the last space.

To cut from the left use split(). To cut once at the last separator and keep it, use rpartition().

Library declarations
python
str.rsplit(sep=None, maxsplit=-1) -> list
# returns: a list of strings
# raises: ValueError if sep is an empty string

Parameters

ParameterDescription
sepOptional. The text to split at. If it is omitted or None, the string is split on runs of whitespace. An empty string raises ValueError.
maxsplitOptional. The most cuts to make, counted from the right. Default -1, which means no limit.

Return Value

Return ValueDescription
listA list of strings, in the same left-to-right order as the original text. The original string is not changed.

Python Examples

Example 1 — Left Cuts and Right Cuts

Split the same text with split() and rsplit(), with and without a limit.

python
text = "a-b-c-d"

print(text.split("-", 1))
print(text.rsplit("-", 1))
print(text.rsplit("-", 2))
print(text.rsplit("-"))

How It Works

  1. split() cuts at the left. One cut at the first hyphen leaves b-c-d together.
  2. rsplit() cuts at the right. One cut at the last hyphen leaves a-b-c together.
  3. Two cuts. With a limit of 2, the last two hyphens are used.
  4. No limit. Without maxsplit every hyphen is a cut, exactly as with split().

Example 2 — Separate Names From Extensions

Cut once from the right, so extra dots in the name do not matter.

python
files = ["photo.jpg", "archive.tar.gz", "README"]

for name in files:
    parts = name.rsplit(".", 1)
    print(parts)

How It Works

  1. One cut from the right. rsplit(".", 1) splits only at the last dot.
  2. Extra dots are safe. archive.tar.gz becomes ['archive.tar', 'gz'], because the earlier dot is not used.
  3. No dot, no cut. README has no dot, so the list holds the whole name as its only item.

Example 3 — Read an Address From the End

Split off the last two parts of an address and unpack them into variables.

python
address = "12 Baker Street, London, UK"
street, city, country = address.rsplit(", ", 2)

print(street)
print(city)
print(country)

How It Works

  1. Two cuts. rsplit(", ", 2) makes at most two cuts, at the last two commas, so the list has three items.
  2. Unpack by position. The three items fill street, city, and country in order.
  3. The street is safe. Even if the street part contained a comma, it would stay in one piece, because only the last two commas are used.

Frequently Asked Questions

It splits a string into a list of substrings, working from the right. It is most useful with maxsplit, to cut at the last separators.
They give the same result when maxsplit is not used. With maxsplit, split() cuts at the first separators and rsplit() cuts at the last ones.
No. The pieces stay in their original left-to-right order.
Use name.rsplit(".", 1). It returns the name and the extension, and it ignores earlier dots. If there is no dot, the list has one item.
No. Strings are immutable, so rsplit() returns a new list and leaves the string as it is.

Next: rstrip()

Return a copy with trailing whitespace, or chosen characters, removed from the right end.

rstrip() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful