Skip to main content

rstrip()

The rstrip() method in Python is a string method that removes any trailing whitespace characters (spaces, tabs, newlines) from the right end of a string. It returns a new string with the whitespace characters removed.

Parameter Values

Parameter Description
chars

(optional) A string specifying the set of characters to be removed from the right end of the string. If not provided, all trailing whitespaces are removed.

Return Values

The rstrip() method returns a new string with trailing whitespace or specified characters removed.

How to Use rstrip() in Python

Example 1:

The rstrip() method removes any trailing characters (characters at the end) from a string. It takes an optional argument specifying the character to remove. If no argument is provided, it will remove whitespace characters by default.

'hello   '.rstrip()
Example 2:

The rstrip() method can be used to remove specific characters from the end of a string. In this example, we remove all trailing '!' characters.

'string!!!'.rstrip('!')
Example 3:

Using rstrip() with a variable to remove specific characters from the end of a string. This example removes all trailing '0' characters.

my_string = '12345000'
print(my_string.rstrip('0'))