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.
The syntax of this method is as follows:
string.rstrip(chars)
The rstrip()
method takes one optional parameter that specifies the trailing character to be removed.
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.
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 removedprint(string.rstrip())string = 'EducativeEEEE'# trailing 'e' is removedprint(string.rstrip('E'))string = 'Educative.......!!!'# trailing 'e' is removedprint(string.rstrip('.!'))