The Python rstrip function is used to remove whitespaces or specified characters from the right-hand side of the string. By default, it removes trailing whitespace characters and returns a new string. However, we can specify the custom characters to delete the trailing characters.
The Python rstrip() function starts looking for the given character on the right-side. Next, it starts deleting those matching characters. The stripping process stops once it encounters a character that is not specified in an argument.
Python rstrip() function syntax
The syntax of the rstrip string function is shown below.
text.rstrip(Chars)
- Text: The original string.
- Chars (optional): If we omit this parameter, it recognizes the white spaces as a default value. To modify, specify the characters to strip from the right side of the string.
Return Value: The rstrip() function does not modify the original string. Instead, it removes trailing characters (spaces or special characters) and returns a copy of the given string.
Python rstrip string function examples
The rstrip method returns a copy of the string with trailing characters removed. The following set of examples helps to understand this function.
Example 1: Removing Trailing Spaces
As we mentioned earlier, the rstrip() function can be used with or without a parameter. In the following example, we declared a string with blank spaces on the right side.
Next, we use the rstrip() method without any arguments. So, the statement removes the white spaces from the right side of a variable Str1 using the rstrip function. The print statement prints the output.
Str1 = 'Tutorial Gateway '
Str2 = Str1.rstrip()
print('Delete White spaces on Right Side using it is =', Str2)
TIP: Use the len() function to find the length of the original string before and after applying the rstrip() function.
The next print statement returns the output in a new string, rather than altering the original. The rstrip() function does not alter the original string, and to change the original, write the following statement.
Str1 = Str1.rstrip()
# Observe the Original
print('Converted is =', Str1.rstrip())
print('Original is =', Str1)
Example 2: Removing Custom Characters
If the original string has unwanted characters on the right side, we can use the Python rstrip() function with a parameter value specifying the character to strip.
Within the following Str3 Python statement, we have zeros on both sides of the website name. Next, we utilized the rstrip() with 0 as the argument value to remove all zeros from it.
If you observe the output image, although there are zeros on both sides, the rstrip() method removed zeros from the Right-hand side only.
# Performing it directly
Str3 = '00000000Tutorial Gateway00000000'.rstrip('0')
print("Deleting 0's on Right Side using it is =", Str3)
NOTE: The rstrip() only removes the given characters from the right side of a string and omits the left-hand side characters.
Example 3: Removing Multiple Characters
Apart from a single character, the Python rstrip() function allows using multiple characters (a substring) as a parameter. If you pass multiple characters, it considers them as individual characters and checks whether any of them are present on the right side. If true, delete them.
In the following string method example code, we used two characters to strip (+ and *) from the right-hand side.
# Remove Right Side
Str4 = 'Tutorial Gateway+++++*********'.rstrip('+*')
print('Deleting + and * on Right Side using it is =', Str4)

Python rstrip remove Newline Character
We can also use the rstrip() function to remove the trailing tab space characters (\t) or newline characters (\n) from the original string.
s = "Happy\n\n"
s2 = s.rstrip()
print(s2)
Happy
Difference between Python rstrip() and strip()
Both built-in string functions are used to strip the extra characters in the original string.
- rstrip(): It only strips the whitespaces or specified characters from the right-side of the string.
- strip(): t removes the leading and trailing blank spaces or specified characters from the string. To strip only the left side, use lstrip().
s = "$$$$$Happy$$$$$$"
print(s.rstrip('$'))
print(s.strip('$'))
$$$$$Happy
Happy