Python3 rstrip() Method
Description
rstrip() removes the specified characters from the end of a string; the default is 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/python3
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
(
'*'
)
)
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!!!
Python3 Strings