VOOZH about

URL: https://www.geeksforgeeks.org/python/python-string-rstrip/

⇱ Python String rstrip() Method - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Python String rstrip() Method

Last Updated : 12 Nov, 2024

The rstrip() method removes trailing whitespace characters from a string. We can also specify custom characters to remove from end of string.

Let's take an example to remove whitespace from the ends of a string.


Output
Hello Python!

Syntax of rstrip() Method

s.rstrip(chars)

  • s: The input string
  • chars (optional): A set of characters to remove as trailing characters

Examples of rstrip() Method

Remove Trailing Whitespaces


Output
 Hello Python!

Remove Custom Characters

If we have a string with various characters that we want to remove from end of the string.


Output
 ##*#Hello Python!

Notes:

  • rstrip('#* ') removes any #, *, and spaces from the end of string.
  • It stops stripping characters from the end of string once it encounters a character that are not in the specified set of characters.

Remove Newline Characters

We can also remove the trailing newline characters (\n) from a string.


Output
Hello Python!
Comment