Python3 rstrip() Method

Python3 字符串Python3 Strings


Description

rstrip() removes the specified characters at 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 -- specifies the characters to be removed (default is whitespace)

Return Value

Returns a new string created by removing the specified characters from the end of the string.

Examples

The following example demonstrates how to use the rstrip() function:

Examples

#!/usr/bin/python3

random_string = 'this is good    '

# Spaces at the end of the string will be removed
print(random_string.rstrip())

# 'si oo' is not a trailing character, so nothing is removed
print(random_string.rstrip('si oo'))

# In 'sid oo', 'd oo' are trailing characters, 'ood' is removed from the string
print(random_string.rstrip('sid oo'))

# 'm/' is a trailing character, no trailing '.' character was found, 'm/' is removed from the string
website = 'www.example.com/'
print(website.rstrip('m/.'))


# Remove comma (,), dot (.), letters s, q or w; these are all trailing characters
txt = "banana,,,,,ssqqqww....."
x = txt.rstrip(",.qsw")
print(x)
# Remove 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.example.co
banana
*****this is string example....wow!!!

Python3 字符串Python3 Strings

Other Extensions