Remember
rstrip() removes trailing whitespace by default. Its chars argument is a set of characters, not a complete suffix.
rstrip() removes trailing whitespace by default. Its chars argument is a set of characters, not a complete suffix.
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 ababa'
result = text.rstrip("ab ")
print(result)Top-level scope
'tea ababa'Object 2 · str'te'rstrip() removes trailing whitespace by default. Its chars argument is a set of characters, not a complete suffix.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
text = 'tea ababa'
result = text.rstrip("ab ")
print(result)
|
v
DISPLAYED RESULT
te
FINAL BINDINGS
text --> object 1 (str)
result --> object 2 (str)
object 1: 'tea ababa'
object 2: 'te'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 ababa'
result = text.rstrip("ab ")
print(result)Top-level scope
'tea ababa'Object 2 · str'te'The example has finished. Compare the final output with the values and references above.
te
What do you expect this program to print?
text = 'tea ababa'
result = text.rstrip("ab ")
print(result)rstrip() MethodThe rstrip() method in Python is used to remove trailing characters (characters at the end) from a string. By default, it removes any whitespace characters (like space, tab, or newline) from the right side of the string.
string.rstrip([chars])
chars (optional) – A string specifying the set of characters to be removed. If omitted, whitespace is removed.text = "Hello World "
result = text.rstrip()
print(result)
Hello World
text = "hello!!!"
result = text.rstrip("!")
print(result)
hello
You can use rstrip() to clean up strings where extra characters are present at the end — for example, when reading data from a file:
line = "data123***\n"
cleaned = line.rstrip("*\n")
print(cleaned)
data123
rstrip() – removes from the right endlstrip() – removes from the left endstrip() – removes from both endsrstrip() removes characters as a set, not a sequence. For example, rstrip("!d") removes any combination of ! or d until it finds a character not in the set.Use rstrip() when working with file lines or logs where trailing newlines or specific characters need to be stripped safely.
rstrip() removes characters from the right side.Write a program to remove all trailing dollar signs '$' from a user-input string.
user_input = input("Enter a string with trailing $: ")
print("Cleaned:", user_input.rstrip("$"))
Connect what you noticed in the example with the idea below.
rstrip() removes trailing whitespace by default. Its chars argument is a set of characters, not a complete suffix.
rstrip("ab") removes only the literal suffix ab.
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.