Python String rstrip() Method Tutorial

In this section, we will learn what String rstrip() method is and how to use it in Python.

What is String rstrip() Method in Python?

The Python string rstrip() method is used to strip the white spaces from the end part of a string value.

This means if the target string value has white spaces at its ending part, then calling this method will clear out those spaces.

Note that by default, this method will remove the ending white spaces, but it also can take an optional argument that accepts a different type of character to be removed from the end part of a string value.

For example, if the ending contains 0s and we want to remove that, then we can put the value 0 as the argument of this method and so this time the method will remove the ending 0s from the string.

Python String rstrip() Method Syntax:

string.rstrip(character)

String rstrip() Method Parameter

The method takes on optional argument and that is the character (or a set of characters) we want to remove from the ending part of a string value. The default value is set to white space.

String rstrip() Method Return Value

The return value of this method is a new string, with its ending part being removed from white spaces (or removed from the value specified as the argument of the method).

Note: the method does not change the original content but instead will create a new one and return that as a result.

Example: using python string rstrip() method

s1 = "*************My name is John Doe**************"

print(s1.rstrip("*"))

Output:

*************My name is John Doe
Facebook
Twitter
Pinterest
LinkedIn

Top Technologies