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:
Machine Learning Mastery with Python - Understand your data, create accurate models and work project...
Python range()
Python Program to Randomly Select an Element From the List
Python Program to Merge Mails
Python Program to Create Pyramid Patterns
Python delattr()
Python Program to Print Hello world!
Python strftime()
Python Program to Parse a String to a Float or Int
Python Errors and Built-in Exceptions
Python Machine Learning Second Edition - Sebastian Raschka & Vahid Mirjalili
Python Program to Convert Celsius To Fahrenheit
Python String isidentifier()
Python Program to Make a Flattened List from Nested List
Python exec()
Python iter()
Reading an HTTP Response Body as a String in Java
Natural Language Processing with Python - Steven Bird & Ewan Klein & Edward Loper
Python Decorators
Python String isalpha()
Python Set discard()
Python Program to Append to a File
Python String islower()
Python frozenset()
Python List count()
Python Program to Count the Number of Each Vowel
Python object()
String Processing with Apache Commons Lang 3
Python String index()
Python Program to Check if a Number is Positive, Negative or 0
Python String capitalize()
Python Set symmetric_difference_update()