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 String lstrip()
Python timestamp to datetime and vice-versa
Introduction to Scientific Programming with Python - Joakim Sundnes
Python Program to Reverse a Number
JavaScript Methods of RegExp and String
Check if a String is a Palindrome in Java
Python time Module
Python List append()
Python bin()
Python Set difference_update()
Python Type Conversion and Type Casting
Python sum()
Python Program to Remove Punctuations From a String
Python Inheritance
Python Program to Merge Two Dictionaries
Python Program to Create a Long Multiline String
Python Program to Shuffle Deck of Cards
Python List clear()
Python range()
Python String lower()
Python Program to Create Pyramid Patterns
Deep Learning with Python - Francois Cholletf
Python frozenset()
Python Tuple
Python Dictionary popitem()
Python Machine Learning Second Edition - Sebastian Raschka & Vahid Mirjalili
Python Program to Check Leap Year
Python getattr()
Python Program to Generate a Random Number
Python Directory and Files Management
Python Input, Output and Import
Python Program to Sort Words in Alphabetic Order