Python String rstrip()

Python String rstrip() method

Python String rstrip() method returns a string with the whitespaces or specified characters removed from the trailing/ending/right-side of given string..

Consider that given string is in x.

x = "  hello world    "

Then the return value of x.rstrip() is

"  hello world"

In this tutorial, you will learn the syntax and usage of String rstrip() method in Python language.

Syntax of rstrip() method

The syntax of String rstrip() method in Python is given below.

str.rstrip(chars)

Parameters

The string rstrip() method takes a single parameter.

ParameterDescription
charsOptional
These specified characters are removed from the right side of the string.
Default value is None.
Python String rstrip() parameters

Return value

The string rstrip() method returns a String value.

Examples

In the following examples, you will learn how to use rstrip() method to strip whitespaces from the right side of the string, and how to use rstrip() method to strip specified characters from the right side of the string.

1. Strip whitespaces from right side of string in Python

In this example, we take a string value in variable x, where string contains some whitespaces at the beginning and ending of the string. We have to remove the whitespaces present at the right side of the string.

Steps

  1. Given a string in x.
  2. Call rstrip() method on the string x. By default, when no argument is specified, the rstrip() method removes whitespaces characters from the right side of the string.
x.rstrip()
  1. The rstrip() method returns a new string, created from removing the whitespace characters from the right side of the original string. Assign the returned value to a variable, say x_stripped.
x_stripped = x.rstrip()
  1. You may print the stripped string to output.

Program

The complete program to strip whitespace characters from the right side of given string.

Python Program

# Given string
x = "  hello world    "

# Strip whitespaces on right side of string
x_stripped = x.rstrip()

print(f"Original       : \"{x}\"")
print(f"Right Stripped : \"{x_stripped}\"")
Run Code Copy

Output

Original       : "  hello world    "
Right Stripped : "  hello world"

Please note that the whitespace characters which are not at the edges remain in the returned string, like the single space between hello and world.

2. Strip specified characters from right side of the string in Python

Instead of whitespace characters, we can also specify characters via argument to rstrip() method, to strip from the right side of the string.

In this example, we take a string value in variable x, and we have to remove the characters ".-," [dot, hyphen, comma] present at the right side or ending of the string.

Steps

  1. Given a string in x.
  2. Call rstrip() method on the string x and pass ".-," for the argument to the rstrip() method.
x.rstrip(".-,")
  1. The rstrip() method returns a new string, created from removing the specified characters, which are dot, hyphen, and comma, from the right end of the string. Assign the returned value to a variable, say x_stripped.
x_stripped = x.rstrip()
  1. You may print the stripped string to output.

Program

The complete program to strip specified characters from the right side of given string.

Python Program

# Given string
x = "......,,hello world----...-"

# Strip specified characters on right side
x_stripped = x.rstrip(".-,")

print(f"Original       : \"{x}\"")
print(f"Right Stripped : \"{x_stripped}\"")
Run Code Copy

Output

Original       : "......,,hello world----...-"
Right Stripped : "......,,hello world"

Summary

In this tutorial of Python String Methods, we learned about String rstrip() method, its syntax, and examples.

Related Tutorials

Code copied to clipboard successfully 👍