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:
# 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')) |
geek
CODE 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 ") |
Geeks for Geeks
CODE 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')) |
geeks for geeks
CODE 4:
# string which is to be stripped string = "geeks for geeks" # Removes given set of characters from # right. print(string.rstrip('ske')) |
geeks for g
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Python String Methods | Set 3 (strip, lstrip, rstrip, min, max, maketrans, translate, replace & expandtabs())
- numpy string operations | rstrip() function
- Python | Pandas Series.str.strip(), lstrip() and rstrip()
- String slicing in Python to check if a string can become empty by recursive deletion
- String slicing in Python to rotate a string
- Python | Check if a given string is binary string or not
- Python | Check if given string can be formed by concatenating string elements of list
- Python | Check if string ends with any string in given list
- Python | Sorting string using order defined by another string
- Python | Merge Tuple String List values to String
- Python | Sort each String in String list
- Python | Convert List of String List to String List
- Python - Length of shortest string in string list
- Python - Remove front K characters from each string in String List
- Python - Remove String from String List
- Python | Delimited String List to String Matrix
- String Alignment in Python f-string
- String to Int and Int to String in Python
- Pad or fill a string by a variable in Python using f-string
- Python - Filter String Tuples if String lengths equals K
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.

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.
