Skip to content
Related Articles

Related Articles

Improve Article

Python String rstrip() Method

  • Last Updated : 19 Aug, 2021

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 stripped
string = "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 stripped
string = "   for    "
 
# Leading whitespaces are removed
print("Geeks" + string.rstrip() + " Geeks ")

Output: 

Geeks   for Geeks

Example 3

Python3




# 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

Python3




# 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

 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. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :