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 strippedstring = "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 strippedstring = "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 strippedstring = "geeks for geeks"# string doesn't have trailing 'e' or 'k'# thus the rstrip() method didn't update the stringprint(string.rstrip('ek')) |
Output:
geeks for geeks
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!


Please Login to comment...