Python | String rstrip()

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

CODE 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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')) 

chevron_right


Output:

geek

CODE 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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 "

chevron_right


Output:

Geeks   for Geeks

CODE 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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')) 

chevron_right


Output:

geeks for geeks

CODE 4:

filter_none

edit
close

play_arrow

link
brightness_4
code

# string which is to be stripped
string = "geeks for geeks"
  
# Removes given set of characters from
# right.
print(string.rstrip('ske')) 

chevron_right


Output:

geeks for g


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.