How to Use the Python rstrip() Function

Contents
In this article, you will learn how to use the Python rstrip() function.
Python rstrip() Function
The Python rstrip() function is a built-in string method that removes all specified characters from the right end of a string. Here’s how to use the rstrip() function:
Syntax
string.rstrip([characters])
Parameters
characters
(optional): A string specifying the set of characters to be removed. If not specified, all whitespace characters will be removed.
Return Value
The rstrip() function returns a new string with all the specified characters removed from the right end of the original string.
Example
Removing Whitespace Characters:
string = " Hello World "
new_string = string.rstrip()
print(new_string)
# Output: " Hello World"
In this example, the rstrip() function removes all the whitespace characters (spaces) from the right end of the string ” Hello World “.
Removing Specific Characters:
string = "Hello World!!!"
new_string = string.rstrip("!")
print(new_string)
# Output: "Hello World"
In this example, the rstrip() function removes all the exclamation marks (!) from the right end of the string “Hello World!!!”.