Python Reference Library →

Python String rstrip() Method – Remove Trailing Characters

Let’s look at string rstrip() method – remove trailing characters

  1. Notice

    rstrip() removes trailing whitespace by default. Its chars argument is a set of characters, not a complete suffix.

  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 ababa'
result = text.rstrip("ab ")
print(result)
Live state

Top-level scope

text→Object 1
result→Object 2
Object 1 · str'tea ababa'Object 2 · str'te'
This step

rstrip() removes trailing whitespace by default. Its chars argument is a set of characters, not a complete suffix.

Picture string rstrip() method – remove trailing characters

rstrip() removes trailing whitespace by default. Its chars argument is a set of characters, not a complete suffix.

Image brief

Alt text
A diagram for Python String rstrip() Method – Remove Trailing Characters. Instructions: text = 'tea ababa' result = text.rstrip("ab ") print(result). Displayed result: te. The final example bindings are text --> object 1 (str); result --> object 2 (str). object 1: 'tea ababa'; object 2: 'te'
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python String rstrip() Method – Remove Trailing Characters". Teaching focus: rstrip() removes trailing whitespace by default. Its chars argument is a set of characters, not a complete suffix. Depict exactly the relationships and labels in this reference diagram: 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' 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 rstrip() Method – Remove Trailing Characters. Instructions: text = 'tea ababa' result = text.rstrip("ab ") print(result). Displayed result: te. The final example bindings are text --> object 1 (str); result --> object 2 (str). object 1: 'tea ababa'; object 2: 'te'

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 ababa'
result = text.rstrip("ab ")
print(result)
Live state

Top-level scope

text→Object 1
result→Object 2
Object 1 · str'tea ababa'Object 2 · str'te'
This step

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

Terminal
Output
te

What do you expect this program to print?

What does this example display?

text = 'tea ababa'
result = text.rstrip("ab ")
print(result)
What does this example display?
Choose one answer. You can try again.

Answer and explanation

te

Follow the printed values in order. The output is: te

Which explanation matches what you learned?

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

Answer and explanation

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.

More examples and reference

Python String rstrip() Method

The 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.

Syntax

string.rstrip([chars])

Parameters:

  • chars (optional) – A string specifying the set of characters to be removed. If omitted, whitespace is removed.

Returns:

  • A copy of the string with trailing characters removed.

Example 1: Remove Trailing Whitespace

text = "Hello World   "
result = text.rstrip()
print(result)
Hello World

Example 2: Remove Specific Trailing Characters

text = "hello!!!"
result = text.rstrip("!")
print(result)
hello

Use Case: Cleaning Up User Input

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

Difference Between rstrip(), lstrip(), and strip()

  • rstrip() – removes from the right end
  • lstrip() – removes from the left end
  • strip() – removes from both ends

Common Mistake

  • Note: rstrip() 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.

Interview Tip

Use rstrip() when working with file lines or logs where trailing newlines or specific characters need to be stripped safely.

Summary

  • rstrip() removes characters from the right side.
  • By default, it removes whitespace.
  • You can specify a custom set of characters to remove.
  • Returns a new string, doesn't change the original.

Practice Problem

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("$"))

What to remember

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

Remember

rstrip() removes trailing whitespace by default. Its chars argument is a set of characters, not a complete suffix.

Watch out for this idea

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.

    Reference library →