Remember
rsplit() splits from the right when maxsplit limits the number of splits. The result is a list.
rsplit() splits from the right when maxsplit limits the number of splits. The result is a list.
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.rsplit(":", 1)
print(result)Top-level scope
'tea:milk:water'Object 2 · list['tea:milk', 'water']rsplit() splits from the right when maxsplit limits the number of splits. The result is a list.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
text = 'tea:milk:water'
result = text.rsplit(":", 1)
print(result)
|
v
DISPLAYED RESULT
['tea:milk', 'water']
FINAL BINDINGS
text --> object 1 (str)
result --> object 2 (list)
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.rsplit(":", 1)
print(result)Top-level scope
'tea:milk:water'Object 2 · list['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.rsplit(":", 1)
print(result)rsplit() MethodThe rsplit() method in Python is used to split a string from the right. It returns a list of substrings, just like split(), but starts splitting from the end of the string.
string.rsplit([separator[, maxsplit]])
separator (optional): The delimiter on which to split the string. If not provided, any whitespace is considered as the separator.maxsplit (optional): Maximum number of splits. The splitting starts from the right.text = "apple,banana,cherry"
result = text.rsplit(",")
print(result)
['apple', 'banana', 'cherry']
text = "apple,banana,cherry"
result = text.rsplit(",", 1)
print(result)
['apple,banana', 'cherry']
Here, only 1 split is done, and it starts from the right.
text = "Python is beginner friendly"
result = text.rsplit()
print(result)
['Python', 'is', 'beginner', 'friendly']
split() splits from the left (start).rsplit() splits from the right (end).maxsplit works from the right.rsplit() to modify the string (it returns a new list).Split the string "2025-05-14" into day, month, and year using rsplit("-", 1). What will be the result?
date = "2025-05-14"
print(date.rsplit("-", 1))
['2025-05', '14']
rsplit() splits a string into a list, starting from the right.maxsplit to control how many splits you want.Connect what you noticed in the example with the idea below.
rsplit() splits from the right when maxsplit limits the number of splits. The result is a list.
rsplit(sep, 1) chooses the leftmost separator.
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.