Skip to content
Related Articles

Related Articles

Python String rstrip() Method

Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 09 Sep, 2022
Improve Article
Save Article

Python String rstrip() method returns a copy of the string with trailing characters removed (based on the string argument passed). If no argument is passed, it removes trailing spaces.

Python String rstrip() Method Syntax

Syntax:  string.rstrip([chars])

Parameters: chars (optional) – a string specifying the set of characters to be removed.

Return: Returns a copy of the string with trailing characters stripped

Python String rstrip() Method Example

Python3




# string which is to be stripped
string = "geekssss"
print(string.rstrip('s'))

Output: 

geek

Example 1: Removing any of matching characters from right of String

Here, in the argument of the Python String rstrip() function, we have passed a String having more than one character. In this case, rstrip() will try to strip the original string of the provided characters from the right side as long as the characters match with any of the characters in the given argument.

Python3




# string which is to be stripped
string = "geeks for geeks"
 
# Removes given set of characters from
# right.
print(string.rstrip('ske'))

Output: 

geeks for g

Example 2: Removing trailing white spaces from string using rstrip() Method

Python3




string = "GeeksForGeeks     "
print(string.rstrip())

Output: 

GeeksForGeeks

Example 3: Python String rstrip() returns the original String if the string cannot be stripped of provided characters

Python3




# string which is to be stripped
string = "geeks for geeks"
 
# string doesn't have trailing 'e' or 'k'
# thus the rstrip() method didn't update the string
print(string.rstrip('ek'))

Output: 

geeks for geeks

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!