Python String rstrip() Method
text.rstrip(chars=None) returns a new string with trailing (right-hand) whitespace, or any characters in chars, removed; the start of the string is untouched.rstrip() is the method you reach for when reading lines from a file or from input(): each line often ends with a newline you do not want. It is also useful for trailing punctuation, extra zeros, or slashes at the end of a path.
For the start of a string use lstrip(); for both ends use strip(). All three leave the original string unchanged.
| Syntax | text.rstrip(chars=None) |
|---|---|
| Parameters | chars (optional): a string whose characters are all removed from the end; default is whitespace |
| Returns | A new str |
| Time complexity | O(n) in the worst case |
How rstrip() removes trailing whitespace and newlines
Python checks the last character, removes it if it is whitespace (or in chars), and repeats until it meets one that stays. That is why rstrip() removes a whole run of trailing newlines or spaces, not just one.
line = "total: 5 \n\n"
print(repr(line))
print(repr(line.rstrip()))
print(repr(line.rstrip("\n")))Passing "\n" removes only newlines and keeps the trailing spaces.
rstrip() with chars removes any listed character
As with the other strip methods, chars is a set of characters, not a suffix. Use it for runs of the same symbol, and use removesuffix() (Python 3.9+) for an exact ending.
print("3.1400".rstrip("0"))
print("Wow!!!".rstrip("!"))
print("report.csv".removesuffix(".csv"))rstrip() vs strip() vs removesuffix()
| Goal | Code | Result |
|---|---|---|
| Drop trailing whitespace | "hi ".rstrip() | 'hi' |
| Drop trailing newline only | "hi\n".rstrip("\n") | 'hi' |
| Drop trailing zeros | "2.500".rstrip("0") | '2.5' |
| Drop an exact ending | "a.py".removesuffix(".py") | 'a' |
| Drop trailing slash | "/docs//".rstrip("/") | '/docs' |
| Both ends | " hi ".strip() | 'hi' |
Examples you can run
Press Run, then edit the code and run it again.
Clean the newline from a line of text
Typical for lines read from a file.
lines = ["apple\n", "kiwi \n", "plum\n"]
for line in lines:
print(f"[{line.rstrip()}]")Expected output[apple]
[kiwi]
[plum]
Trim trailing zeros from a number string
Only the end is changed.
price = "12.5000"
print(price.rstrip("0"))
print(price.rstrip("0").rstrip("."))
print("1500".rstrip("0"))Expected output12.5
12.5
15
Normalise a folder path
Remove extra slashes at the end so paths join cleanly.
folder = "/home/asha/projects///"
clean = folder.rstrip("/")
print(clean)
print(clean + "/notes.txt")Expected output/home/asha/projects
/home/asha/projects/notes.txt
Common mistakes
Stripping zeros from whole numbers
✗ Wrong
print("1500".rstrip("0"))✓ Right
price = "12.500"
print(price.rstrip("0"))Only strip zeros after a decimal point, or format the number instead.
Using rstrip() to remove a file extension
✗ Wrong
print("data.txt".rstrip(".txt"))✓ Right
print("context.txt".removesuffix(".txt"))Use removesuffix() for an exact ending.
Ignoring the return value
✗ Wrong
s = "hi "
s.rstrip()
print(repr(s))✓ Right
s = "hi "
s = s.rstrip()
print(repr(s))Assign it back: s = s.rstrip().
Python String rstrip() Method: questions people ask
How do I remove the newline at the end of a string in Python?
Use text.rstrip("\n") to remove only newlines, or text.rstrip() to remove all trailing whitespace including newlines.
What is the difference between rstrip() and strip()?
rstrip() cleans only the right end of the string, while strip() cleans both the left and the right.
Does rstrip() remove a suffix?
No, it removes any trailing characters that appear in its argument. For an exact suffix use removesuffix(), available since Python 3.9.
Keep learning
Step-by-step lessons:
Python StringsPython String MethodsPython File Handling
Related:
Python String strip() MethodPython String lstrip() MethodHow to Remove Whitespace from a String in PythonPython String splitlines() Method
More in string methods:
Python String split() MethodPython String join() MethodPython String strip() MethodPython String lstrip() MethodPython String replace() MethodPython String upper() MethodPython String lower() MethodPython String title() Method See all →