str.rstrip()

Added in v3.x · Updated March 13, 2026 · String Methods
stdlib string methods

The .rstrip() method returns a copy of the string with trailing characters removed. By default, it removes whitespace characters—spaces, newlines, tabs, and other whitespace. You can pass a string argument to specify exactly which characters to strip from the end.

This method is essential for cleaning up data from files, processing user input, and handling formatted strings where trailing characters need to be removed.

Signature

str.rstrip([chars])

Parameters

ParameterTypeDefaultDescription
charsstrNoneA string specifying the set of characters to remove. If None, all trailing whitespace characters are removed.

Return Value

Returns a new string with trailing characters removed. The original string remains unchanged because strings are immutable in Python.

Return type: str

Examples

Basic Usage

>>> "hello  ".rstrip()
'hello'

>>> "hello\n\t".rstrip()
'hello'

>>> "data   \n\n   ".rstrip()
'data'

By default, .rstrip() removes spaces, newlines, tabs, and other whitespace from the right side of the string.

Stripping Specific Characters

>>> "hello---".rstrip('-')
'hello'

>>> "data___".rstrip('_')
'data'

>>> "valuexxx".rstrip('x')
'value'

When you provide the chars argument, the method removes any combination of those characters from the end. It keeps stripping until it encounters a character not in the set.

Multiple Character Types

>>> "hello***!!!".rstrip('*!')
'hello'

>>> "text----====".rstrip('=-')
'text'

>>> "data##@@".rstrip('#@')
'data'

The chars parameter treats each character as an individual removal target, not as a substring.

Practical Use Cases

Cleaning file output

with open("output.txt") as f:
    for line in f:
        line = line.rstrip()  # Remove trailing newline/space
        if line:
            print(line)

When reading lines from a file, each line typically ends with a newline character. .rstrip() removes it cleanly.

Processing user input

user_input = "password123   "
cleaned = user_input.rstrip()
print(cleaned)  # Output: 'password123'

This is useful when users accidentally add spaces at the end of their input.

Handling formatted data

>>> "42".rstrip()
'42'

>>> "3.14   ".rstrip()
'3.14'

>>> "item-----".rstrip('-')
'item'

Cleaning numeric strings or removing delimiters from data is straightforward.

Combining with other methods

# Remove trailing whitespace, then split
>>> "  hello world  ".rstrip().split()
['hello', 'world']

# Chain with lstrip for full stripping
>>> "   hello   ".rstrip().lstrip()
'hello'

# Or use strip() which does both
>>> "   hello   ".strip()
'hello'

How It Works

The method scans from the end of the string, removing characters that appear in the chars set. Once it encounters a character not in the set, it stops.

>>> "helloaa".rstrip('a')
'hello'

>>> "xxxxword".rstrip('x')
'word'

>>> "ababab".rstrip('ab')
''  # All characters removed

>>> "abcxyz".rstrip('ab')
'abcxyz'  # Stops at 'c', which is not in the set

The order of characters in the chars argument doesn’t matter. Python treats it as a set of characters to remove.

Edge Cases

>>> "".rstrip()
''

>>> "   ".rstrip()
''

>>> "hello".rstrip()
'hello'  # No trailing whitespace to remove

Common Mistakes

Assuming it removes from the middle:

>>> "hello world".rstrip()
'hello world'  # Space in middle is NOT removed

# Use replace() for middle spaces:
>>> "hello world".replace(" ", "")
'helloworld'

Forgetting it returns a new string:

s = "hello   "
s.rstrip()              # Returns new string, s unchanged
s = s.rstrip()         # Assign result to keep it

Confusing with JavaScript:

# Python's rstrip() = JavaScript's trimEnd() or trimRight()
# Both remove trailing whitespace

Performance

.rstrip() is implemented in C and runs in O(n) time where n is the number of characters removed from the end. For typical whitespace removal, the overhead is negligible.

See Also