Table of contents
1.
Introduction
2.
Python String rstrip() Method Syntax
2.1.
Python
3.
Parameters
3.1.
1. `chars` (optional)
3.2.
Python
4.
Return
4.1.
Python
5.
Python String rstrip() Method Example
5.1.
Example 1: Removing any of matching characters from right of String
5.2.
Python
5.3.
Example 2: Removing trailing white spaces from string using rstrip() Method
5.4.
Python
5.5.
Example 3: Python String rstrip() returns the original String if the string cannot be stripped of provided characters
5.6.
Python
6.
Frequently Asked Questions
6.1.
What happens if the chars parameter is not provided to the rstrip() method?
6.2.
Does the rstrip() method modify the original string?
6.3.
What happens if the rstrip() method cannot find any matching characters to remove?
7.
Conclusion
Last Updated: Aug 2, 2024
Easy

Python String rstrip() Method

Author Sinki Kumari
0 upvote

Introduction

In Python, strings are a fundamental data type that are used to store & manipulate text. Python have many inbuilt functions and one of them is the rstrip() method function that removes characters from the right side of a string. It's a very useful tool for cleaning up unwanted trailing characters, such as whitespace or specific characters. 

Python String rstrip() Method

In this article, we'll talk about the syntax, it’s parameters, & use of the rstrip() method, along with examples which will help you understand how to apply it effectively in your Python programs.

Python String rstrip() Method Syntax

The syntax for the rstrip() method is :

string.rstrip([chars])


The `string` represents the string object on which you want to apply the rstrip() method. The `chars` parameter is optional & specifies the set of characters to be removed from the right side of the string. If `chars` is not provided, rstrip() removes trailing whitespace characters by default, including spaces, tabs, & newlines.

For example : 

  • Python

Python

text = "   Hello, World!   "

cleaned_text = text.rstrip()

print(cleaned_text)
You can also try this code with Online Python Compiler
Run Code


Output:

Hello, World!


In this example, the rstrip() method is called on the `text` string without specifying any `chars` parameter. It removes the trailing whitespace characters from the right side of the string, resulting in `"   Hello, World!"` with the trailing spaces removed.

Parameters

The rstrip() method accepts one optional parameter:

1. `chars` (optional)

A string specifying the set of characters to be removed from the right side of the string. If omitted or `None`, rstrip() removes trailing whitespace characters by default.


The `chars` parameter allows you to specify a custom set of characters to be stripped from the right side of the string. It works as follows:

- If `chars` is provided, rstrip() removes any trailing characters that are present in the `chars` string.
 

- The characters in `chars` are treated individually, not as a substring. For example, `rstrip("abc")` removes any trailing occurrences of 'a', 'b', or 'c', not the substring "abc".
 

- The order of characters in `chars` doesn't matter. rstrip() removes all occurrences of each character from the right side of the string.


For example : 

  • Python

Python

text = "Hello, World!..."

cleaned_text = text.rstrip(".!")

print(cleaned_text)
You can also try this code with Online Python Compiler
Run Code

Output

Hello, World


In this example, the rstrip() method is called with the `chars` parameter set to `".!"`. It removes any trailing occurrences of the characters '.' and '!' from the right side of the string, resulting in `"Hello, World"`.

Return

The rstrip() method returns a new string with the specified characters removed from the right side of the original string. It does not modify the original string.

- If `chars` is provided & trailing characters are found, rstrip() returns a new string with those characters removed from the right side.

- If `chars` is not provided or `None`, rstrip() returns a new string with trailing whitespace characters removed from the right side.

- If no trailing characters are found that match the `chars` parameter or there are no trailing whitespace characters, rstrip() returns the original string unchanged.

Let’s look at a few examples to show the return behavior of strip():

  • Python

Python

text1 = "   Hello, World!   "

cleaned_text1 = text1.rstrip()

print(cleaned_text1) 


text2 = "Hello, World!..."

cleaned_text2 = text2.rstrip(".!")

print(cleaned_text2) 


text3 = "Hello, World!"

cleaned_text3 = text3.rstrip("XYZ")

print(cleaned_text3)
You can also try this code with Online Python Compiler
Run Code


Output

"   Hello, World!"
"Hello, World!"
"Hello, World!"


In the first example, rstrip() removes the trailing whitespace characters from the right side of `text1`, returning `"   Hello, World!"`.

In the second example, rstrip() removes the trailing '.' & '!' characters from the right side of `text2`, returning `"Hello, World"`.

In the third example, rstrip() doesn't find any trailing characters that match the `chars` parameter (`"XYZ"`), so it returns the original string `"Hello, World!"` unchanged.

Python String rstrip() Method Example

Now let's see few more examples to understand the functioning of the rstrip() method in different scenarios.

Example 1: Removing any of matching characters from right of String

  • Python

Python

text = "Hello, World!..."

cleaned_text = text.rstrip(".!")

print(cleaned_text)
You can also try this code with Online Python Compiler
Run Code


Output

Hello, World


In this example, the rstrip() method is called with the `chars` parameter set to `".!"`. It removes any trailing occurrences of the characters '.' and '!' from the right side of the string `"Hello, World!..."`, resulting in `"Hello, World"`.

Example 2: Removing trailing white spaces from string using rstrip() Method

  • Python

Python

text = "   Hello, World!   "

cleaned_text = text.rstrip()

print(cleaned_text)

print(repr(cleaned_text))  # Using repr() to show the actual string representation
You can also try this code with Online Python Compiler
Run Code


Output

  Hello, Wrld!
'   Hello, World!'


In this example, the rstrip() method is called without specifying any `chars` parameter. By default, it removes any trailing whitespace characters from the right side of the string `"   Hello, World!   "`. The resulting string is `"   Hello, World!"` with the trailing spaces removed.

The `repr()` function is used to display the actual string representation, including the leading spaces, to clearly show the effect of rstrip().

Example 3: Python String rstrip() returns the original String if the string cannot be stripped of provided characters

  • Python

Python

text = "Hello, World!"

cleaned_text = text.rstrip("XYZ")

print(cleaned_text)
You can also try this code with Online Python Compiler
Run Code


Output

Hello, World!


In this example, the rstrip() method is called with the `chars` parameter set to `"XYZ"`. However, the string `"Hello, World!"` does not have any trailing characters that match the specified characters in `"XYZ"`. Therefore, rstrip() returns the original string unchanged.

This example shows that if the rstrip() method cannot find any matching characters to remove from the right side of the string, it simply returns the original string without any modifications.

Frequently Asked Questions

What happens if the chars parameter is not provided to the rstrip() method?

If the chars parameter is not provided or is None, the rstrip() method removes any trailing whitespace characters (spaces, tabs, newlines) from the right side of the string.

Does the rstrip() method modify the original string?

No, the rstrip() method does not modify the original string. It returns a new string with the specified characters removed from the right side of the original string.

What happens if the rstrip() method cannot find any matching characters to remove?

If the rstrip() method cannot find any trailing characters that match the specified chars parameter or there are no trailing whitespace characters, it returns the original string unchanged.

Conclusion

In this article, we have learned about the Python string rstrip() method, which is used to remove trailing characters from the right side of a string. We saw its syntax, parameters, & return value. We also explained several examples which shows how to use rstrip() to remove specific characters or whitespace from the right side of a string. The rstrip() method provides a convenient way to clean up & format strings by removing unwanted trailing characters.

You can also practice coding questions commonly asked in interviews on Coding Ninjas Code360

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass