Python 3 reference · String Methods

Python String rstrip() Method

Return a copy with trailing characters removed: whitespace by default, or any of the characters you pass.

Python 3 Since 3.0

Syntax

str.rstrip(chars=None)

Parameters

ParameterDescription
chars optional A set of characters to remove. Default: whitespace.

Return value

A new string.

Examples

Strip the right sideTry it yourself
print(repr("line with newline\n".rstrip()))
print("3.1400".rstrip("0"))

Output

'line with newline'
3.14
chars is a set, not a suffixTry it yourself
print("lists.csv".rstrip(".csv"))  # also eats the final s of "lists"!
print("lists.csv".removesuffix(".csv"))

Output

list
lists

Version history

VersionChange
Python 3.0Added