Python String rstrip() Method

Python rstrip() method is used to get a copy of the string from which all trailing characters or white spaces on the right side of the string have been removed. By default, it removes white spaces.

Syntax:

string_var.rstrip(characters)
characters(optional) Parameter is a string containing characters to be removed from the end of the string. if not mentioned, white spaces are removed
Return Value:

The rstrip() returns a string with all trailing characters(given as argument) or white spaces removed from the right side of the string.

In The following example, you can see that in the output of the rstrip() method, trailing white space characters have been removed from string

			

# Python example of the rstrip() method, 

string = "tutorialsnation   " 
# Removes spaces from left.
print(string.rstrip())


				

Output:

tutorialsnation

The following example removes multiple characters from the end of the string

			

# Python example of the rstrip() method, 

string = "vtutorialsnation$x++==--a@" 
# Removes spaces from right.
print(string.rstrip("$x+=-a@"))


				

Output:

tutorialsnation