Friday, December 27, 2024
Google search engine
HomeLanguagesPython String rstrip() Method

Python String rstrip() Method

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 = "Lazyroarsss"
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 = "Lazyroar for Lazyroar"
 
# Removes given set of characters from
# right.
print(string.rstrip('ske'))


Output: 

Lazyroar 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 = "Lazyroar for Lazyroar"
 
# string doesn't have trailing 'e' or 'k'
# thus the rstrip() method didn't update the string
print(string.rstrip('ek'))


Output: 

Lazyroar for Lazyroar

RELATED ARTICLES

Most Popular

Recent Comments