The rstrip method

The rstrip method returns a string with characters removed from the end. In the optional parameter of the method, we specify the characters we want to remove. If they are not specified, the method will only remove spaces at the end of the string.

Syntax

pythonstring.rstrip([characters to delete])

Example

Let's apply the rstrip method to the following line:

pythontxt = 'abcdea'
print(txt.rstrip('a'))

Result of code execution:

python'abcde'

Example

Let's put spaces at the beginning and end of the string and apply the rstrip method again:

pythontxt = ' abcdea '
print(txt.rstrip('a'))

Since the last character of the string is now a space rather than the specified 'a', the method, finding no matches, simply returns the original string:

python' abcdea '

Example

Now let's apply the rstrip method without specifying the parameter:

pythontxt = ' abcdea '
print(txt.rstrip())

Result of code execution:

python' abcdea'

See also

  • method strip,
    which removes characters from the beginning and end of a string
  • method lstrip,
    which removes characters from the beginning of a line