English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Python Basic Tutorial

Python Flow Control

Python Functions

Python Data Types

Python File Operations

Python Objects and Classes

Python Date and Time

Advanced Python Knowledge

Python Reference Manual

Python string rstrip() usage and example

Python string methods

rstrip() method removes the specified characters (default is space) at the end of the string.

rstrip() removes characters from the right side according to the parameter (a string specifying the set of characters to be deleted).

The syntax of rstrip():

string.rstrip([chars])

rstrip() parameter

  • chars (optional)-A string specifying the set of characters to be deleted.

If no chars parameter is provided, all spaces on the right side of the string are removed.

rstrip() return value 

rstrip() returns a copy of the string with the trailing characters removed.

chars remove all combinations of characters from the right side of the string until the first mismatch.

Example: The operation of rstrip()

random_string = ' this is good'
# Remove leading whitespace
print(random_string.rstrip())
# The parameter does not contain 'd'
# Do not delete any characters.
print(random_string.rstrip('si oo'))
print(random_string.rstrip('sid oo'))
website = 'www.w'3codebox.com/'
print(website.rstrip('m/.'))

When running the program, the output is:

 this is good
 this is good
 this is g
www.w3codebox.co

Python string methods