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.
Syntax:
string.rstrip([chars])
Parameters:
chars (optional) – a string specifying the set of characters to be removed.
Returns:
Returns a copy of the string with trailing characters stripped
Example 1
Python3
# Python3 program to demonstrate the use of # rstrip() method using optional parameters # string which is to be strippedstring = "geekssss"
# Removes given set of characters from# right.print(string.rstrip('s'))
|
Output:
geek
Example 2
Python3
# Python3 program to demonstrate the use of # rstrip() method using optional parameters # string which is to be strippedstring = " for "
# Leading whitespaces are removedprint("Geeks" + string.rstrip() + " Geeks ")
|
Output:
Geeks for Geeks
Example 3
Python3
# string which is to be strippedstring = "geeks for geeks"
# Argument doesn't contain trailing 's'# So, no characters are removedprint(string.rstrip('ek'))
|
Output:
geeks for geeks
Example 4
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

