Python rstrip() method
Description
Python rstrip() removes specified characters from the end of the string, defaulting to whitespace, including spaces, newlines, carriage returns, and tabs.
Syntax
Syntax of the rstrip() method:
str.rstrip([chars])
Parameters
- chars -- the characters to remove (defaults to whitespace)
Return Value
Returns the new string generated after removing the specified characters from the end of the string.
Example
The following example shows how to use the rstrip() function:
Example
#!/usr/bin/python
-- coding: UTF-8 --
random_string
=
'this is good '
Trailing spaces in the string are removed
(
random_string.
rstrip
(
)
)
'si oo' is not a trailing character sequence, so nothing is removed
(
random_string.
rstrip
(
'si oo'
)
)
In 'sid oo', 'd oo' is trailing, so 'ood' is removed from the string
(
random_string.
rstrip
(
'sid oo'
)
)
'm/' is trailing and no trailing '.' is found, so 'm/' is removed from the string
website
=
'www.runoob.com/'
(
website.
rstrip
(
'm/.'
)
)
Remove commas (,), periods (.), and the letters s, q or w, all of which are trailing characters
txt
=
"banana,,,,,ssqqqww....."
x
=
txt.
rstrip
(
",.qsw"
)
(
x
)
Remove the trailing character *
str
=
"***this is string example....wow!!!***"
(
str
.
rstrip
(
'*'
)
)
(
x
)
The output of the above example is:
this is good
this is good
this is g
www.runoob.co
banana
*****this is string example....wow!!!
Python Strings