Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
whitespace
trailing
character
communitycreator

What is rstrip() method in Python?

Mahnoor Zaheer

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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)
The syntax of rstrp() method in Python

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('.!'))
The syntax of rstrip() method in Python

RELATED TAGS

python
whitespace
trailing
character
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring