Python String rstrip() Method (original) (raw)

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.

Python `

s = "Hello Python! " res = s.rstrip() print(res)

`

Syntax of rstrip() Method

s.rstrip(_chars)

Table of Content

Examples of rstrip() Method

Remove Trailing Whitespaces

Python `

s = " Hello Python! " res = s.rstrip() print(res)

`

Remove Custom Characters

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

Python `

s = ' ##*#Hello Python!#**## '

removes all occurrences of '#', '*', and ' '

from the end of string

res = s.rstrip('#* ') print(res)

`

**Notes:

Remove Newline Characters

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

Python `

s = '\nHello Python!\n'

Removing newline characters

from the end of string

res = s.rstrip()

print(res)

`