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 Multiply Two Matrices
Python Functions
Python Global, Local and Nonlocal variables
Python Program to Find Sum of Natural Numbers Using Recursion
Python Program to Make a Flattened List from Nested List
Python String capitalize()
Python Program to Find Numbers Divisible by Another Number
Convert char to String in Java
Machine Learning Mastery with Python - Understand your data, create accurate models and work project...
Python tuple()
Count Occurrences of a Char in a String
Python Program to Access Index of a List Using for Loop
Python zip()
String Initialization in Java
Python String translate()
Deep Learning with Python - Francois Cholletf
Intelligent Projects Using Python - Santanu Pattanayak
Python String isalnum()
Python String split()
Python Program to Get the Full Path of the Current Working Directory
Deep Learning from Scratch - Building with Python form First Principles - Seth Weidman
Python range()
Python Anonymous / Lambda Function
Python memoryview()
Node.js vs Python for Backend Development
Python Program to Find the Sum of Natural Numbers
Python String encode()
Python Program to Create a Long Multiline String
APIs in Node.js vs Python - A Comparison
Python *args and **kwargs
Python if...else Statement
Python Program to Display Calendar