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
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
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
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
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
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 Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.