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 Capitalize the First Character of a String
Deep Learning in Python - LazyProgrammer
Python Statement, Indentation and Comments
Python List append()
Python RegEx
Python Dictionary get()
Python Program to Generate a Random Number
Python Program to Check Whether a String is Palindrome or Not
Python String translate()
Python Shallow Copy and Deep Copy
Python List reverse()
Python Program Read a File Line by Line Into a List
Python Program to Catch Multiple Exceptions in One Line
Python String isnumeric()
Python Program to Merge Mails
Python String lstrip()
Python Program to Measure the Elapsed Time in Python
Python Program to Get the Full Path of the Current Working Directory
Python Operators
Python ascii()
Python List Comprehension
Python Objects and Classes
Convert String to int or Integer in Java
Python Program to Convert Decimal to Binary Using Recursion
Python complex()
Python hash()
Python dir()
Python String split()
Python String index()
Python Machine Learning Eqution Reference - Sebastian Raschka
Python String format()
Python String endswith()