Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python3 rstrip() Method

Python3 rstrip() Method

Python3 Strings Python3 Strings


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

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

print

(

random_string.

rstrip

(

)

)

'si oo' is not a trailing character sequence, so nothing is removed

print

(

random_string.

rstrip

(

'si oo'

)

)

In 'sid oo', 'd oo' is trailing, so 'ood' is removed from the string

print

(

random_string.

rstrip

(

'sid oo'

)

)

'm/' is trailing and no trailing '.' is found, so 'm/' is removed from the string

website

=

'www.runoob.com/'

print

(

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"

)

print

(

x

)

Remove the trailing character *

str

=

"***this is string example....wow!!!***"

print

(

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 Python3 Strings