Python String rstrip() Method

By Lenin Mishra

If you prefer to watch Youtube videos over reading blogs, check out our video on Python strings here.

Python String rstrip() method

The rstrip() method in Python, returns a new string by removing(stripping) all the characters from the right that are passed as string argument. If no arguments are passed, it removes the trailing spaces

Syntax of rstrip() method
string.rstrip()

Returns a new string
Example

Code/Output

x = "Pylenin loves Python   "
print(x.rstrip())
>>> Pylenin loves Python

x = "*Pylenin loves Python*"
print(x.rstrip('*'))
>>> *Pylenin loves Python

x = "*@Pylenin loves Python@*"
print(x.rstrip('@'))
>>> *@Pylenin loves Python@*

As you can see, the rstrip() method didn’t work on the third string. The @ has to be trailing in order to be stripped.

Check out other commonly used Python string methods.

  1. How to create a string in Python?
  2. How to access characters in a Python string?
  3. How to replace characters in a string in Python?
  4. How to concatenate strings in Python?
  5. How to iterate through a string in Python?
  6. Check if a Substring is Present in a Given String in Python
  7. Escape sequences in Python String
  8. Python String Formatting - The Definitive Guide