Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
string
characters
communitycreator

What is the string.rstrip method in Python?

Programming Bytes

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 will remove a set of trailing characters in a string and return a new string.

Syntax

string.rstrip([chars_to_remove])

The rstrip method will loop through all the characters of the string from the end of the string and check if the current character of the string is present in the passed chars_to_remove.

  • If the current character is present in chars_to_remove, then that character will be removed.

  • If the current character is not present, then the looping is stopped.

Parameters

chars: This is the set of characters to be removed from the trailing positionend of the string. It is an optional argument. By default, the trailing spaces are removed.

Return value

This method returns a new string in which the passed set of characters is removed.

Example 1

string = "   space at the end is removed      ";
string.rstrip();

In the code above, we have a string with whitespace at the start and end of the string. When we call the rstrip method without any arguments, the space at the end of the string is removed.

Example 2

string = "sun raises";
string.rstrip("s"); #sun raise

In the code above, we have a sun raises string and call the rstrip('s') method. The rstrip method will loop the string sun raises in reverse order:

  • Last first char: s is present in the passed argument so it is removed from the string. Now, the string is sun raise.

  • Last second char: e is not present in the passed argument, so looping is stopped and the sun raise string is returned.

Example 3

string = "sun raises";
string.rstrip("seria ");

In the code above, we have a sun raises string and call the rstrip('seria ') method. The rstrip method will loop the string sun raises in reverse order:

  • Last first char: s is present in the 'seria ' so it is removed from the string. Now the result string is sun raise.

  • Last second char: e is present in the 'seria ', so it is removed from the string. Now the result string is sun rais'.

  • Last third char: s is present in the 'seria ' so it is removed from the string. Now the result string is sun rai.

  • Last fourth, fifth, sixth, and seventh characters are i ,a, r, and whitespace respectively. All these characters are present in 'seria ', so these characters are removed. Now the string will be sun.

  • Last eighth char: n is not present in the 'seria ', so looping is removed. The result string sun is returned.

Complete example

# Example 1
string = " space at the end is removed "
print(string.rstrip()); #space at the end is removed
# Example 2
string = "sun raises";
print(string.rstrip("s")); #sun raise
# Example 3
string = "sun raises";
print(string.rstrip("seria ")); #sun

RELATED TAGS

python
string
characters
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