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 Set intersection_update()
Python Program to Count the Number of Each Vowel
Python slice()
Python String lstrip()
Python Program to Convert Celsius To Fahrenheit
Machine Learning Applications Using Python - Cases studies form Healthcare, Retail, and Finance - Pu...
Python all()
Python if...else Statement
Python String isprintable()
Python frozenset()
Python help()
Python Program to Display Fibonacci Sequence Using Recursion
Python del Statement
Python getattr()
Convert String to int or Integer in Java
Python Shallow Copy and Deep Copy
Python Dictionary keys()
Python Program to Generate a Random Number
Python int()
Python List clear()
Python chr()
JavaScript Eval: run a code string
Split a String in Java
Python Exception Handling Using try, except and finally statement
Python Global, Local and Nonlocal variables
Python String translate()
Python complex()
Python Program to Find Numbers Divisible by Another Number
Python Dictionary fromkeys()
Python String isalpha()
Python oct()
Python Set copy()