Python - String rstrip() Method


Advertisements

The string rstrip() method is used to trim the string from right side only. This method has one optional parameter which is used to specify trim character(s). Default value of this parameter is whitespace.

Syntax


string.rstrip(character(s))

Parameters

character(s) Optional. Character(s) which need to be trimmed from right side of the string. default value is whitespace.

Example:

In the below example, rstrip() method to used to trim the specified characters from the right side of the string.

MyString = "   Hello World!   "
#trim whitespaces from right side of the string
print(MyString.rstrip())

MyString = "@*Hello, World*@"
#trim * and @ from right side of the string
print(MyString.rstrip("*@"))

Output

   Hello World!
   
@*Hello, World

String Methods
Advertisements