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 = "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 = "geeks for geeks"
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 = "geeks for geeks"
print (string.rstrip( 'ek' ))
|
Output:
geeks for geeks
Last Updated :
09 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...