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

Python basic tutorial

Python flow control

Fonctions en Python

Types de données en Python

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 deletes the specified characters (default is space) from the end of the string.

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

The syntax of rstrip() is:

string.rstrip([chars])

rstrip() parameter

  • chars (optional)-A string that specifies the set of characters to be removed.

If no chars parameter is provided, all spaces on the right 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 end of the string until the first non-matching.

Example: The work 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