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:
Convert char to String in Java
Python Program to Get the Class Name of an Instance
Python timestamp to datetime and vice-versa
Python Closures
Python Dictionary items()
Python Program to Count the Number of Digits Present In a Number
Python Set intersection_update()
How to get current date and time in Python?
Python Set symmetric_difference_update()
Python Program to Shuffle Deck of Cards
Python Program to Display Fibonacci Sequence Using Recursion
String Initialization in Java
Case-Insensitive String Matching in Java
Python Program to Convert Decimal to Binary, Octal and Hexadecimal
Statistical Methods for Machine Learning - Disconver how to Transform data into Knowledge with Pytho...
Python any()
Python Program to Convert Kilometers to Miles
Python String title()
Python id()
Python Program to Count the Number of Occurrence of a Character in String
Python ascii()
Intelligent Projects Using Python - Santanu Pattanayak
Python Program to Parse a String to a Float or Int
Python Program to Slice Lists
Python Machine Learning Cookbook - Practical solutions from preprocessing to Deep Learning - Chris A...
Python Program to Compute all the Permutation of the String
Deep Learning with Python - Francois Cholletf
Java String to InputStream
Python Dictionary setdefault()
Python sum()
Reading an HTTP Response Body as a String in Java
Python List extend()