The Wayback Machine - http://web.archive.org/web/20220218214816/https://www.geeksforgeeks.org/python-string-rstrip/amp/

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.

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 program to demonstrate the use of
# rstrip() method using optional parameters
 
# string which is to be stripped
string = "geekssss"
 
# Removes given set of characters from
# right.
print(string.rstrip('s'))

Output: 

geek

Example 2




# Python3 program to demonstrate the use of
# rstrip() method using optional parameters
 
# string which is to be stripped
string = "   for    "
 
# Leading whitespaces are removed
print("Geeks" + string.rstrip() + " Geeks ")

Output: 

Geeks   for Geeks

Example 3




# string which is to be stripped
string = "geeks for geeks"
 
# Argument doesn't contain trailing 's'
# So, no characters are removed
print(string.rstrip('ek'))

Output: 

geeks for geeks

Example 4




# 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




Article Tags :