Python rstrip() Function - Explained with Examples

The rstrip() function in Python allows you to remove the characters you specify at the end of a string. If no characters are specified, it will remove blank spaces. Whitespace is the default character that the function will remove.

This is a useful function to remove unwanted characters quickly. 

Here at Ibmi Media, we shall look into how to use the rstrip() function in Python.

More about rstrip() function ?

The rstrip() function removes any character you specify. It only removes the characters at the end.

Whitespace is the default character that the function will remove.

It's syntax is given below:


rstrip() function Parameter Values:

chars: the characters you request to remove

Examples of using Python rstrip() Function

1. Check the below function:

txt = " canada "
x = txt.rstrip()

The output will give:


2. Another function is given below:

str = "pythonaaaa"
x = str.rstrip('a')

The output will give:


3. Also, see the below function:

str = "cateri,."
x = str.rstrip("eir,.")

It's output will give:


[Need help in fixing Python issues ? We can help you. ]


This article covers how to use the Python String rstrip() Method. In fact, the rstrip() method returns a copy of the string by removing the trailing characters specified as argument. If the characters argument is not provided, all trailing whitespaces are removed from the string.

Python String rstrip() Method Syntax:


rstrip() Method Parameters:

characters: (optional) A char or string to be removed from the end of the string.