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 Matrices and NumPy Arrays
Python String isalnum()
Python Anonymous / Lambda Function
Python super()
Python Dictionary values()
Python Program to Shuffle Deck of Cards
Python String encode()
Python String split()
Python Set issubset()
Python Program to Print Hello world!
Python Program to Swap Two Variables
Python list()
Python Program to Check if a Key is Already Present in a Dictionary
Python memoryview()
Python String rfind()
Python Functions
Python Data Structures and Algorithms - Benjamin Baka
Python Program to Find Hash of File
Converting String to Stream of chars
Applied Text Analysis with Python - Benjamin Benfort & Rebecca Bibro & Tony Ojeda
Format ZonedDateTime to String
Python String swapcase()
Convert char to String in Java
Python Program to Convert Decimal to Binary, Octal and Hexadecimal
Python Program to Split a List Into Evenly Sized Chunks
Python hex()
Python String rindex()
Python Program to Convert String to Datetime
Converting a Stack Trace to a String in Java
Python exec()
Python Program to Find LCM
Adding a Newline Character to a String in Java