Skip to main content

How to Remove Characters from the End of a String in Python with rstrip

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

rstrip returns a copy of a string with characters removed from its end: whitespace when you call it with no argument, or any of the characters you list. It works on bytes too, which makes it handy for dropping = padding from base64.

Syntax​

Syntax
<result> = <text>.rstrip()
<result> = <text>.rstrip("<characters>")
<result> = <data>.rstrip(b"<characters>")
PartWhat it does
.rstrip()Removes spaces, tabs and line breaks from the end.
"<characters>"A set of characters: rstrip keeps removing from the end while the last character is any one of them.
b"<characters>"The same for a bytes value, which needs a bytes argument.

The Python documentation for str.rstrip: "Return a copy of the string with trailing characters removed. The chars argument is a string specifying the set of characters to be removed."

The Example​

The example strips spaces and a line break from the end of a word, the exclamation marks from Wow!!!, the = padding from a base64url value held as bytes, and .txt from test.txt, which goes wrong in an instructive way.

Prerequisites​

  • uv and Python. This guide uses uv 0.13.10 with Python 3.15.2.

Step 1: Write the Code​

Create a file named main.py:

main.py, which prints the repr of hello with trailing spaces and a newline after rstrip, Wow!!! after rstrip of the exclamation mark, the bytes SGk_Pg== after rstrip of the equals sign, and test.txt after rstrip of .txt.

main.py
print(repr("hello \n".rstrip()))
print("Wow!!!".rstrip("!"))
print(b"SGk_Pg==".rstrip(b"="))
print("test.txt".rstrip(".txt"))

Step 2: Run It​

Terminal
uv run python main.py
Output
'hello'
Wow
b'SGk_Pg'
tes

A zsh terminal where uv run python main.py prints 'hello', Wow, b'SGk_Pg' and tes.

The command is the same in zsh, bash and PowerShell.

What Happened​

  • rstrip() removed the three spaces and the line break; repr shows the quotes so you can see nothing is left.
  • rstrip("!") removed all three exclamation marks, not just one.
  • rstrip(b"=") removed both = from the bytes.
  • rstrip(".txt") didn't remove the text .txt. It removed every t, x and . from the end until it reached the s, leaving tes.

Common Mistakes​

MistakeWhat happensDo this instead
Using rstrip to remove a word or file extensionIt treats the argument as a set of characters: "test.txt".rstrip(".txt") gave tesUse removesuffix(".txt"), which gave test
Passing a bytes argument to a str, or the other way roundTypeError: rstrip arg must be None or strMatch the types: str with str, bytes with bytes
Expecting the original string to changeStrings never change; rstrip returns a new oneAssign the result: text = text.rstrip()

Troubleshooting​

SymptomLikely causeFix
More characters disappeared than expectedCharacters in the argument also end the real textUse removesuffix
Nothing changedThe characters weren't at the very end, or you didn't keep the resultCheck with repr(), and assign the result

FAQ​

How do I strip both ends?​

strip() removes from both ends and lstrip() from the start only. They take the same optional set of characters.

Why does rstrip("na") turn banana into b?​

Because it keeps removing while the last character is n or a: "banana".rstrip("na") gave b.

Conclusion​

text.rstrip() trims whitespace from the end, and text.rstrip(chars) trims any of the given characters, as many as it finds. To remove one exact ending, use removesuffix instead.

Additional Resources​