Tutorial Study Image

Python rstrip()

The rstrip() function in python removes the trailing characters in the copy of the original string based on the arguments given. The method returns this copy as output.

string.rstrip([chars]) #where chars are those to remove from right

rstrip() Parameters:

The rstrip() function takes a set of characters as its parameter. If chars are not provided the trailing space is removed from the string.

Parameter Description Required / Optional
chars characters to remove as trailing characters Optional

rstrip() Return Value

The return value is always a string. Until the first match found, all combinations of characters are removed from the right onwards.

Input Return Value
string copy of string

Examples of rstrip() method in Python

Example 1: How rstrip() works in Python?

string1 = "     python     "

string2 = string1.rstrip()

print("of all programming languages", string2, "is my favorite")


of all programming languages     python is my favorite

Example 2: Working of rstrip() in Python

# Variable declaration  
string1 = "Python and Java* "  
# Calling function  
string2 = string1.rstrip(*)  
# Displaying result  
print("Before strip: ",string1)  
print("After strip: ",string2)  


Before strip:  Python and Java* 
After strip:  Python and Java