Methods and Functions

Python String rstrip()

When parsing a string, we may encounter a situation in which we might have extra whitespaces or characters that are unnecessary and we would want to remove them. The Python stripping functions are used to tackle this problem. They are useful and convenient in such situations. Python language provides 3 types of stripping functions. One of them being the Python rstrip() function. In this article, we will learn more about the Python rstrip() string method, its applications, and examples.

Python rstrip() function

Definition

  • Python rstrip() function is used to return a new string by removing the trailing characters (based on the argument passed) from the input string.
  • The Python rstrip() string method returns a copy of the string by removing all the characters (specified in the argument) from the right end of the input string.

rstrip() Syntax

Syntax followed by the rstrip() function is:

                    

string.rstrip([chars])

rstrip() Parameters

The rstrip() function accepts a single parameter, which is also an optional parameter.

  • chars (Optional) – a string specifying the set of trailing characters to be removed from the input string

Note – If the chars parameter is not specified, the rightmost whitespaces of the string are eliminated. i.e. whitespaces are removed by default.

The whitespaces in Python are:

  1. ‘ ‘ – Space
  2. ‘\t’ – Horizontal tab
  3. ‘\v’ – Vertical tab
  4. ‘\n’ – Newline
  5. ‘\r’ – Carriage return
  6. ‘\f’ – Feed

Return value from rstrip()

The rstrip() function returns a duplicate of the string with the trailing characters removed.

Until the first mismatch, all combinations of characters in the chars parameter are eliminated from the string’s right side.

Example 1: Working of rstrip()

Example

                    

# Python program to illustrate rstrip()
# trailing whitespaces removed
txt = '   Good Morning   '
print('Original string:', txt)
print('New string:', txt.rstrip())
print('')

# 'm' trailing character not removed
txt = 'Python Programming'
print('Original string:', txt)
print('New string:', txt.rstrip('m'))
print('')

# 'ing' trailing character removed
txt = 'Python Programming'
print('Original string:', txt)
print('New string:', txt.rstrip('ing'))

Output

                    

Original string:    Good Morning   
New string:    Good Morning

Original string: Python Programming
New string: Python Programming

Original string: Python Programming
New string: Python Programm

Example 2: How does rstrip() work?

As illustrated below, you can specify one or more characters as a string to be removed from the string in any sequence.

Example

                    

# Python program to illustrate rstrip()
txt = '123486491212321'
print('Original string:', txt)
print('New string:', txt.rstrip('123'))
print('')

txt = '@$^*@@&&'
print('Original string:', txt)
print('New string:', txt.rstrip('&@'))
print('')

txt = 'abcabcabcabc'
print('Original string:', txt)
print('New string:', txt.rstrip('cb'))

Output

                    

Original string: 123486491212321
New string: 12348649

Original string: @$^*@@&&
New string: @$^*

Original string: abcabcabcabc
New string: abcabcabca

We can define our own characters to be removed. For example, txt.rstrip(‘123’) removes all the trailing characters ‘1’, ‘2’, and ‘3’ from the string in any order of their occurrences.

Frequently Asked Questions

Q1. What is rstrip in Python and why is it used?

When parsing a string, we may encounter a situation in which we might have extra whitespaces or characters that are unnecessary and we would want to remove them. Python rstrip() function is used to return a new string by removing the trailing characters (based on the argument passed) from the input string.

Q2. What are lstrip and rstrip in Python?

Python language provides 3 types of stripping functions. Two of them being Python rstrip() and lstrip() function.

  • Python rstrip() – removes all trailing whitespaces or custom strings specified as arguments to the function.
  • Python lstrip() – removes all leading whitespaces or custom strings specified as arguments to the function.

Example

                    

txt = '***Hello***'
print('Original string:', txt)
print('New rstrip string:', txt.rstrip('*'))
print('New lstrip string:', txt.lstrip('*'))
print('')

txt = 'www.PythonProgram.com/'
print('Original string:', txt)
print('New rstrip string:', txt.rstrip('com/'))
print('New lstrip string:', txt.lstrip('w'))

Output

                    

Original string: ***Hello***
New rstrip string: ***Hello
New lstrip string: Hello***

Original string: www.PythonProgram.com/
New rstrip string: www.PythonProgram.
New lstrip string: .PythonProgram.com/

Q3. What does split() do in Python?

The split() method splits a string at the specified separator and returns a list of strings. If no separator is supplied, any whitespace string is used.

Syntax:

                    

string.split(separator, maxsplit)

Example

                    

txt = 'Car, Bus, Plane, Boat, Cycle, Bike'
# splits at space
print(txt.split())

# maxsplit : 3
print(txt.split(',', 3))

Output

                    

['Car,', 'Bus,', 'Plane,', 'Boat,', 'Cycle,', 'Bike']
['Car', ' Bus', ' Plane', ' Boat, Cycle, Bike']

Q4. What are max and min in Python?

  • Python max() – The Python max() function takes an input and returns the item with the highest value in that iterable. If the argument passed is a string, then alphabetically the largest value is been displayed. When multiple iterables are provided as parameters, the greatest value of the list among the other list items is returned.
  • Python min() – Python min() built-in function is used to return the smallest item from the passed iterable objects. If the argument passed is a string, then alphabetically the smallest value is been displayed. It can also be used to determine the smallest object with respect to two or more parameters.

Example

                    

num_list = [4, 6, 9, 2, 0, -3, -5]
print('Number list:', num_list)
print('Largest number is:', max(num_list))
print('Smallest number is:', min(num_list))
print('')

cars = ['Audi', 'Jaguar', 'Ferrari', 'Volvo', 'BMW']
print('Cars list:', cars)
print('Largest string is:', max(cars))
print('Smallest string is:', min(cars))

Output

                    

Number list: [4, 6, 9, 2, 0, -3, -5]
Largest number is: 9
Smallest number is: -5

Cars list: ['Audi', 'Jaguar', 'Ferrari', 'Volvo', 'BMW']
Largest string is: Volvo
Smallest string is: Audi

Q5. How do you find the minimum of a list in Python?

To find out the minimum value in a list, we use the Python min() function. This function returns the smallest value compared to other values in the list. If the list contains string values, then alphabetically smallest letter is compared and returned accordingly.

Example

                    

num_list = [45, 734, 72, 19, 69, 28, 59]
print('Number list:', num_list)
print('Smallest number is:', min(num_list))
print('')

food = ['Cake', 'Bread', 'Apple', 'Milk', 'Tea']
print('Cars list:', food)
print('Smallest string is:', min(food))

Output

                    

Number list: [45, 734, 72, 19, 69, 28, 59]
Smallest number is: 19

Cars list: ['Cake', 'Bread', 'Apple', 'Milk', 'Tea']
Smallest string is: Apple

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.