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 LinuxAPT, 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:

string.rstrip(chars)


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()
print(x)

The output will give:

canada


2. Another function is given below:

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

The output will give:

python


3. Also, see the below function:

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

It's output will give:

cat


[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:

str.rstrip(characters)

rstrip() Method Parameters:

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

Related Posts