What is rstrip() method in Python?

The rstrip() method removes the specified trailing characters from the end of the string. If no argument is specified, it removes the trailing whitespaces from the end.

Syntax

The syntax of this method is as follows:

string.rstrip(chars)

Parameters

The rstrip()method takes one optional parameter that specifies the trailing character to be removed.

Return value

The return value is a copy of the string with the removed trailing character. The rstrip() method does not change the original string but creates a copy of it.

Code

When no argument is specified for the rstrip() method in the code below, it removes the trailing whitespaces. The specified trailing characters, i.e., e and .!, are removed from string in the second and third examples.

string = 'Educative '
# Whitespaces are removed
print(string.rstrip())
string = 'EducativeEEEE'
# trailing 'e' is removed
print(string.rstrip('E'))
string = 'Educative.......!!!'
# trailing 'e' is removed
print(string.rstrip('.!'))

Free Resources