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 Program to Get a Substring of a String
Python Program to Get Line Count of a File
Python Program to Differentiate Between del, remove, and pop on a List
Case-Insensitive String Matching in Java
Python divmod()
Python Program to Print the Fibonacci sequence
Python Machine Learning Third Edition - Sebastian Raschka & Vahid Mirjalili
Python zip()
Python str()
Python String isdigit()
Python del Statement
Python Program to Generate a Random Number
Natural Language Processing with Python - Steven Bird & Ewan Klein & Edward Loper
Python staticmethod()
Adding a Newline Character to a String in Java
Python Program to Catch Multiple Exceptions in One Line
Java – Reader to String
Python Set pop()
Python vars()
Why String is Immutable in Java?
Python Data Structures and Algorithms - Benjamin Baka
Python Object Oriented Programming
Python Program to Display Powers of 2 Using Anonymous Function
Python Program to Convert String to Datetime
Deep Learning with Python - Francois Cholletf
Python Generators
Python List Comprehension
Python List count()
Python range()
APIs in Node.js vs Python - A Comparison
Python Operator Overloading
Python Program to Merge Mails