Python String rstrip() Method

Python String rstrip() Method

In this tutorial, we will learn about rstrip() Method in Python. The rstrip() method in Python is like a tool to tidy up strings. It takes away extra spaces, tabs, and newlines from the right side of a string. For instance, if you have a sentence with spaces at the end like. ” This is an example string with trailing spaces. “, you can use rstrip() to get rid of those spaces.

This method helps you handle and clean up the text. When you have data with unnecessary spaces or tabs towards the end.

RELATED POST: Python String lower() method

rstrip() method Syntax

new_string = original_string.rstrip([characters])

Parameters Details:

  • original_string: This is the string from which you want to remove trailing whitespace.
  • characters (optional): A string containing the specific characters you want to remove from the right side of the original string. If not provided, the method will remove all whitespace characters.

Python rstrip() method return value

The rstrip() method in Python returns a new string with trailing (right-side) whitespace characters removed. It does not modify the original string; instead, it creates and returns a modified copy of the string.

Check out My Latest post on Developer Helps for some Interesting Insights


Python String rstrip() method Example:

Below are the different examples of the Python String rstrip() method.

  • Removing trailing spaces from a string by using rstrip() method.
  • Removing specific characters from the right side of a string by using rstrip() String method.
  • Combining rstrip() with reassignment to modify the original string.
  • Using rstrip() to remove trailing tabs from a string.
  • Chaining multiple String methods including rstrip().
  • By using rstrip() with a string containing various whitespace characters.

Removing trailing spaces from a Python String

text = "   Hello, World!   "
new_text = text.rstrip()
print(new_text)


Output:

"   Hello, World!"

In this example, the rstrip() method removes the trailing spaces from the text string. The result new_text is a new string with the modified content, leaving the original text unchanged.

Removing specific characters from the right side of a String

text = "ABC123$$$"
new_text = text.rstrip("$")
print(new_text)


Output:

"ABC123"

Combining rstrip() with reassignment to modify the original string

text = "   Trim me!    "
text = text.rstrip()
print(text)  


Output:

"   Trim me!"

By using rstrip() to remove trailing tabs from a Python String

code = "\tif x > 10:\t\t\t"
clean_code = code.rstrip("\t")
print(clean_code)


Output:

"\tif x > 10:"

Only the trailing tabs were removed because the "\t" argument was passed to rstrip().

Chaining multiple String methods including rstrip()

data = "   42 apples   \n"
cleaned_data = data.strip().upper().rstrip("S")
print(cleaned_data)  


Output:

"42 APPLE"

By using rstrip() with a string containing various whitespace characters

content = "This is a sentence.\n\t  "
trimmed_content = content.rstrip("\n\t ")
print(trimmed_content) 


Output:

"This is a sentence."

The rstrip() method returns a new string with the specified modifications while leaving the original string unchanged. If you want to update the original string, you need to reassign the result back to the original variable.

Discover Our Exciting Courses and Quiz

Enroll now to enhance your skills and knowledge!

Python Online Quiz

Level up your coding skills with our interactive programming quiz!