Python String rpartition() Method – Split String from the Right

Python String rpartition() Method

The rpartition() method in Python splits a string into three parts using a specified separator, but it starts the search from the right side of the string.

Syntax

string.rpartition(separator)

Parameters:

  • separator – The string to search for and split at.

Returns:

  • A tuple of three strings: (head, separator, tail)
  • If the separator is not found, it returns ('' , '', original_string)

Example 1: Separator Found

text = "learn.python.with.fun"
result = text.rpartition(".")
print(result)
('learn.python.with', '.', 'fun')

Example 2: Separator Not Found

text = "hello world"
result = text.rpartition("-")
print(result)
('', '', 'hello world')

Use Case: File Path or Version Extraction

filename = "report.v2.2025.pdf"
name, dot, extension = filename.rpartition(".")
print(extension)
pdf

Great for extracting the last extension or versioning information.

Difference from partition()

  • partition() splits at the first occurrence.
  • rpartition() splits at the last occurrence.

Common Mistakes

  • Forgetting that the separator is case-sensitive.
  • Expecting multiple splits – it only splits once, into three parts.

Interview Tip

rpartition() is handy when parsing URLs, file paths, or logs that have predictable suffixes.

Summary

  • rpartition() splits a string into 3 parts from the right.
  • Returns a tuple: (before_separator, separator, after_separator)
  • If not found, returns ('', '', original_string)

Practice Problem

Write a program to extract the last name from a full name using rpartition():

full_name = "Ada Lovelace"
before, space, last_name = full_name.rpartition(" ")
print("Last name is:", last_name)