rstrip() removes characters from the right end (the finish) of a string. With no argument it removes trailing whitespace, including the newline that ends a line read from a file. The left end and the middle are left alone.
Pass a string of characters to remove them instead. The argument is a set of characters, not a suffix, and the method keeps removing for as long as the text ends with one of them. That is why "report.txt".rstrip(".txt") gives the surprising "repor". The result is always a new string.
text.rstrip() is a method of the built-in str type: call it on a string with dot notation. No import is needed, and it never changes the original string.
Syntax
Call it with no argument to trim trailing whitespace, or pass the characters to remove.
python
clean = text.rstrip()
clean = text.rstrip(chars)
Call
What it does
"hi \n".rstrip()
Returns 'hi'. The spaces and the newline on the right are removed.
"3.1400".rstrip("0")
Returns '3.14'. The trailing zeros are removed.
"report.txt".rstrip(".txt")
Returns 'repor'. The argument is a set of characters, so the t at the end of report is removed too.
Use lstrip() for the left end and strip() for both ends. To remove an exact suffix such as a file extension, use removesuffix().
Library declarations
python
str.rstrip([chars]) -> str
# returns: a new string; the original is unchanged
# chars: a string of characters to remove; default is whitespace
Parameters
Parameter
Description
chars
Optional. A string whose characters are removed from the right end. The order does not matter. If it is omitted or None, whitespace is removed.
Return Value
Return Value
Description
str
A new string with the matching characters removed from the end. The original string is not changed.
Python Examples
Example 1 — Remove the Newline From a Line
Strip trailing whitespace and keep the leading spaces.
Right side only. The two trailing spaces and the newline are removed.
The left side stays. The two leading spaces are still there, so startswith(" ") is True.
A common job. Lines read from a file end with a newline, and rstrip() is the usual way to remove it.
Example 2 — Remove Trailing Zeros From a Number
Strip the zeros, then the decimal point if nothing remains after it.
python
for text in ["12.500", "12.000", "700"]:
tidy = text.rstrip("0").rstrip(".")
print(text, tidy)
Output
12.500 12.5
12.000 12
700 7
How It Works
Zeros first.rstrip("0") turns "12.500" into "12.5" and "12.000" into "12.".
Then the dot. The second rstrip(".") removes the leftover decimal point, giving "12".
A trap for whole numbers."700" has no decimal point, but its trailing zeros are still removed, giving "7". Only use this on numbers with a decimal point.
Example 3 — Why rstrip() Cannot Remove an Extension
See the wrong result, then fix it with an endswith() check and a slice.
python
name = "report.txt"
print(name.rstrip(".txt"))
if name.endswith(".txt"):
print(name[:-4])
Output
repor
report
How It Works
The wrong result. The argument ".txt" is the set ., t, x, so the final t of report is removed as well.
Check, then slice.endswith(".txt") confirms the suffix, and name[:-4] drops exactly the last four characters.
The right result. The slice keeps the whole word report.
Frequently Asked Questions
It returns a copy of the string with trailing characters removed from the right end. By default it removes whitespace, including a newline.
Call line.rstrip("\n") to remove only newlines, or line.rstrip() to remove all trailing whitespace.
The argument is a set of characters, not a suffix, so every trailing t, x, t, and dot is removed, including the t that ends the word report. Use removesuffix() instead.
rstrip() trims only the right end, while strip() trims both the left and the right end.
No. Strings are immutable, so rstrip() returns a new string. Assign the result to keep it.