Python String rstrip()

The Python String rstrip() method is a built-in function that strips trailing characters based on the arguments passed to the function and returns the copy of a string.

In this article, we will learn about the Python String rstrip() method with the help of examples.

rstrip() Syntax

The Syntax of rstrip() method is:


rstrip() Parameters

The rstrip() method takes one parameter, and it’s optional.

  • chars(optional) – set of characters representing string that needs to be removed from the right-hand side of the string. 

If the chars argument is not passed, the rstrip() function will strip whitespaces at the end of the string. 

rstrip() Return Value

The rstrip() method returns a copy of the string by stripping the trailing characters based on the arguments passed.


  • If we do not pass any arguments to rstrip() function, by default, all trailing whitespaces are truncated from a string.
  • If the string does not have any whitespaces at the end, the string will be returned as-is, matching the original string.
  • If the characters passed in the arguments do not match the characters at the end of the string, it will stop removing the trailing characters.

Example 1: Working of rstrip()

# Only trailing whitespaces are removed
text1 = '   Python Programming   '

# Remove the whitespace and specified character at
# trailing end
text2 = '       code its my code        '
print(text2.rstrip(' code'))

# Remove the specified character at 
# trailing end
text3 = 'code its my code'


   Python Programming
       code its my
code its my

Example 2 – How to use rstrip() method in real world?

In the below example, we have a list of the price in dollars. However the dollar sign is appended at both the trailing and leading end of each element. We can simply iterate the list and remove the dollar symbol at the right hand side using the rstrip() method as shown below.

price = ['$100$', '$200$', '$300$', '$400$', '$500$']
new_price = []
for l in price:



['$100', '$200', '$300', '$400', '$500']
Leave a Reply

Your email address will not be published.

Sign Up for Our Newsletters

Get notified of the best deals on our WordPress themes.

You May Also Like
Python String Zfill()

Python String zfill()

Table of Contents Hide zfill() Syntaxzfill() Parameterszfill() Return ValueExample 1: How zfill() works in Python?Example 2: zfill() in case of Sign Prefix The Python String zfill() method is a built-in…
View Post
Python String Upper()

Python String upper()

Table of Contents Hide upper() Syntaxupper() Parameterupper() Return ValueExample 1: Convert a string to uppercaseExample 2: How upper() is used in a program?Example 3: How to check if two strings…
View Post
Python Write Text File

Python Write Text File

Table of Contents Hide Steps on How to write to a File in Python Python open() function Methods for Writing to a text file in PythonPython close() functionExamples for Writing to…
View Post
Python List Count()

Python List count()

Table of Contents Hide Syntax of List count() count() ParametersReturn Value from List count()Example 1: Use of count()Example 2: Count Tuple and List Elements Inside List Python List count() is a built-in…
View Post
Python String Swapcase()

Python String swapcase()

Table of Contents Hide swapcase() Syntaxswapcase() Parametersswapcase() Return ValueExample: Python Program to change the case of a given string Python string swapcase() method is a built-in function that converts all…
View Post