Remember
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.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
text = 'tea:milk:water'
result = text.rpartition(":")
print(result)Top-level scope
'tea:milk:water'Object 2 · tuple('tea:milk', ':', 'water')rpartition() splits at the last separator and always returns a three-part tuple.
(no output)
What do you expect this program to print?
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')Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
text = 'tea:milk:water'
result = text.rpartition(":")
print(result)Top-level scope
'tea:milk:water'Object 2 · tuple('tea:milk', ':', 'water')The example has finished. Compare the final output with the values and references above.
('tea:milk', ':', 'water')
What do you expect this program to print?
text = 'tea:milk:water'
result = text.rpartition(":")
print(result)rpartition() MethodThe 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.
string.rpartition(separator)
separator – The string to search for and split at.(head, separator, tail)('' , '', original_string)text = "learn.python.with.fun"
result = text.rpartition(".")
print(result)
('learn.python.with', '.', 'fun')
text = "hello world"
result = text.rpartition("-")
print(result)
('', '', 'hello world')
filename = "report.v2.2025.pdf"
name, dot, extension = filename.rpartition(".")
print(extension)
pdf
Great for extracting the last extension or versioning information.
partition() splits at the first occurrence.rpartition() splits at the last occurrence.rpartition() is handy when parsing URLs, file paths, or logs that have predictable suffixes.
rpartition() splits a string into 3 parts from the right.(before_separator, separator, after_separator)('', '', original_string)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)
Connect what you noticed in the example with the idea below.
rpartition() splits at the last separator and always returns a three-part tuple.
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.