Python rpartition() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

rpartition() works like partition(), but it searches from the right, and cuts the string at the last appearance of the separator. It always returns a tuple of three items: the part before, the separator, and the part after.

If the separator is not found, you still get three items, but the whole string comes last: two empty strings, then the text. That detail matters when you unpack the result. The original string is not changed.

text.rpartition() 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 the separator to look for. It is required, and it must be a string.

python
before, sep, after = text.rpartition(sep)
CallWhat it does
"a/b/c".rpartition("/")Returns ('a/b', '/', 'c'). The text is cut at the last slash.
"key=a=b".rpartition("=")Returns ('key=a', '=', 'b'). Only the last equals sign is used.
"novalue".rpartition("=")Returns ('', '', 'novalue'). The separator is missing, so the whole text is the last item.

To cut at the first separator use partition(). To get a list and limit the cuts from the right, use rsplit(). To only find the position of the last match, use rfind().

Library declarations
python
str.rpartition(sep) -> tuple
# returns: (before, sep, after), a tuple of three strings

Parameters

ParameterDescription
sepRequired. The text to split at. It must be a non-empty string, otherwise ValueError is raised.

Return Value

Return ValueDescription
tupleA tuple of three strings: the part before the last separator, the separator, and the part after. If the separator is not found, it is ('', '', text).

Python Examples

Example 1 — Split a Folder From a File Name

Cut a path at its last slash, and compare with partition().

python
path = "/usr/local/bin/python"
folder, sep, name = path.rpartition("/")

print(folder)
print(name)
print(path.partition("/"))

How It Works

  1. The last slash. rpartition() cuts at the final /, so the folder is everything before it.
  2. The file name. The part after the last slash is python.
  3. partition() uses the first slash. The first character is a slash, so the part before it is empty and the rest stays together.

Example 2 — The Missing Separator Trap

Split names at the last dot, and see where a name without a dot ends up.

python
for text in ["archive.tar.gz", "README"]:
    base, dot, ext = text.rpartition(".")
    print(repr(base), repr(ext))

How It Works

  1. A normal name. archive.tar.gz is cut at the last dot, giving 'archive.tar' and 'gz'.
  2. No dot. README has no dot, so base and dot are empty strings.
  3. The text moves to the end. ext becomes 'README', which is wrong for an extension. Check if dot: before you trust it.

Example 3 — Read the Last Part of a Version

Cut a version string at the last dot and use the last part as a number.

python
version = "3.12.4"
major_minor, dot, patch = version.rpartition(".")

print(major_minor)
print(patch)
print(int(patch) + 1)

How It Works

  1. The last dot. The text is cut only at the final dot, so 3.12 stays together.
  2. The last part. patch is the string '4'.
  3. Convert, then add. int(patch) + 1 turns the text into a number and adds one, which gives 5.

Frequently Asked Questions

It splits a string at the last occurrence of a separator and returns a tuple of three strings: the part before, the separator, and the part after.
It returns a tuple of two empty strings followed by the whole text, such as ("", "", text).
partition() uses the first separator and rpartition() uses the last one. If the separator is missing, partition() puts the text first and rpartition() puts it last.
Use name.rpartition(".")[2], but check that the middle item is not empty first, because a name without a dot would give the whole name.
No. Strings are immutable, so rpartition() returns a new tuple and leaves the string as it is.

Next: rsplit()

Break a string into a list of pieces, splitting from the right.

rsplit() 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.

10 people found this page helpful