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
| Parameter | Description |
|---|---|
chars optional |
A set of characters to remove. Default: whitespace. |
Return value
A new string.
Examples
print(repr("line with newline\n".rstrip()))
print("3.1400".rstrip("0"))
Output
'line with newline' 3.14
print("lists.csv".rstrip(".csv")) # also eats the final s of "lists"!
print("lists.csv".removesuffix(".csv"))
Output
list lists
Version history
| Version | Change |
|---|---|
| Python 3.0 | Added |
Related
str.lstrip()· Return a copy with leading characters removed: whitespace by default, or any of the characters you pass.str.strip()· Return a copy with leading and trailing characters removed: whitespace by default, or any of the characters you pass.str.removesuffix()· Return the string without the given suffix if it ends with it; otherwise return it unchanged.- Python Strings · tutorial