Table of Contents
In this tutorial, we will learn about the Python String rstrip() method with the help of examples.
The rstrip() method returns a copy of the string with trailing characters removed (based on the string argument passed).
Example
title = 'Python Programming ' # remove trailing whitespace print(title.rstrip()) # Output: Python Programming
1. Syntax of String rstrip()
The syntax of rstrip() is:
string.rstrip([chars])
2. rstrip() Parameters
- chars (optional) – a string specifying the set of trailing characters to be removed.
The rstrip() removes characters from the right based on the argument (a string specifying the set of characters to be removed).
If chars argument is not provided, all whitespaces on the right are removed from the string.
3. Return Value from rstrip()
The rstrip() returns a copy of the string with trailing characters stripped.
All combinations of characters in chars argument are removed from the right of the string until the first mismatch.
4. Example: Working of rstrip()
random_string = 'this is good '
# Trailing whitespace are removed
print(random_string.rstrip())
# 'si oo' are not trailing characters so nothing is removed
print(random_string.rstrip('si oo'))
# in 'sid oo', 'd oo' are the trailing characters, 'ood' is removed from the string
print(random_string.rstrip('sid oo')) website = 'www.maixuanviet.com'
print(website.rstrip('m/.'))
Output
this is good this is good this is g www.maixuanviet.com
Related posts:
Python *args and **kwargs
Python Program to Get a Substring of a String
Python Program to Copy a File
Python slice()
Python Program to Check if a Number is Odd or Even
Deep Learning with Python - Francois Cholletf
Python divmod()
Python Deep Learning - Valentino Zocca & Gianmario Spacagna & Daniel Slater & Peter Roelants
Python Object Oriented Programming
Python Program to Print all Prime Numbers in an Interval
Split a String in Java
Python Program to Iterate Over Dictionaries Using for Loop
Python String lstrip()
Generate a String
Python hasattr()
CharSequence vs. String in Java
Python String lower()
Deep Learning from Scratch - Building with Python form First Principles - Seth Weidman
Python String splitlines()
Python Program to Create a Long Multiline String
Python callable()
Python List copy()
Python Program to Iterate Through Two Lists in Parallel
Python Program to Find HCF or GCD
Python Function Arguments
Python String ljust()
Python String istitle()
Python Program to Compute the Power of a Number
Python abs()
Python Program to Find the Factors of a Number
Python String swapcase()
Python Program to Print Output Without a Newline