Python Reference Library →

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

Let’s look at string rpartition() method – split string from the right

  1. Notice

    rpartition() splits at the last separator and always returns a three-part tuple.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
text = 'tea:milk:water'
result = text.rpartition(":")
print(result)
Live state

Top-level scope

text→Object 1
result→Object 2
Object 1 · str'tea:milk:water'Object 2 · tuple('tea:milk', ':', 'water')
This step

rpartition() splits at the last separator and always returns a three-part tuple.

Picture string rpartition() method – split string from the right

rpartition() splits at the last separator and always returns a three-part tuple.

Image brief

Alt text
A diagram for Python String rpartition() Method – Split String from the Right. Instructions: text = 'tea:milk:water' result = text.rpartition(":") print(result). Displayed result: ('tea:milk', ':', 'water'). The final example bindings are text --> object 1 (str); result --> object 2 (tuple). object 1: 'tea:milk:water'; object 2: ('tea:milk', ':', 'water')
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python String rpartition() Method – Split String from the Right". Teaching focus: rpartition() splits at the last separator and always returns a three-part tuple. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS text = 'tea:milk:water' result = text.rpartition(":") print(result) | v DISPLAYED RESULT ('tea:milk', ':', 'water') FINAL BINDINGS text --> object 1 (str) result --> object 2 (tuple) object 1: 'tea:milk:water' object 2: ('tea:milk', ':', 'water') Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python String rpartition() Method – Split String from the Right. Instructions: text = 'tea:milk:water' result = text.rpartition(":") print(result). Displayed result: ('tea:milk', ':', 'water'). The final example bindings are text --> object 1 (str); result --> object 2 (tuple). object 1: 'tea:milk:water'; object 2: ('tea:milk', ':', 'water')

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
text = 'tea:milk:water'
result = text.rpartition(":")
print(result)
Live state

Top-level scope

text→Object 1
result→Object 2
Object 1 · str'tea:milk:water'Object 2 · tuple('tea:milk', ':', 'water')
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
('tea:milk', ':', 'water')

What do you expect this program to print?

What does this example display?

text = 'tea:milk:water'
result = text.rpartition(":")
print(result)
What does this example display?
Choose one answer. You can try again.

Answer and explanation

('tea:milk', ':', 'water')

Follow the printed values in order. The output is: ('tea:milk', ':', 'water')

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

rpartition() splits at the last separator and always returns a three-part tuple.

rpartition() splits at the last separator and always returns a three-part tuple.

More examples and reference

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)

What to remember

Connect what you noticed in the example with the idea below.

Remember

rpartition() splits at the last separator and always returns a three-part tuple.

Watch out for this idea

rpartition() splits at the first occurrence of the separator.

This is the misconception to avoid.

Reference checks are optional and do not count toward course progress.

    Reference library →