Python String | rstrip method
Start your free 7-days trial now!
Python's str.rstrip(~)
method returns a string with the specified trailing characters removed. Default is to remove all trailing spaces (i.e. spaces on the right).
Parameters
1. chars
| string
| optional
The string specifying the combination of trailing characters to remove from right of the string. Default is whitespace.
Return value
A string with the specified trailing characters stripped.
Examples
Basic usage
To strip all trailing whitespace from "Dog "
:
x = "Dog "x.rstrip()
'Dog'
Chars parameter
To remove all combinations of trailing ' re'
from "SkyTowner "
:
y = "SkyTowner "y.rstrip(' re')
'SkyTown'
We remove all combinations of ' '
, 'r'
and 'e'
from the end of the string which gives us return value of 'SkyTown'
.
Case sensitivity
To remove all trailing 'R'
from "SkyTowner":
z = "SkyTowner"z.rstrip('R')
'SkyTowner'
Note that no trailing characters are removed as there is a mismatch at the very right of the string where char
argument of 'R'
does not match with 'r'
. As this shows, rstrip(~)
method is case sensitive.