Python 3 教程 Python3 rstrip() 方法

brian · 2022-03-13 18:12:47 · 热度: 174

描述

rstrip() 删除 string 字符串末尾的指定字符,默认为空白符,包括空格、换行符、回车符、制表符。

语法

rstrip()方法语法:

str.rstrip([chars])

参数

  • chars -- 指定删除的字符(默认为空白符)

返回值

返回删除 string 字符串末尾的指定字符后生成的新字符串。

实例

以下实例展示了 rstrip() 函数的使用方法:

实例

#!/usr/bin/python3

random_string = 'this is good    '

# 字符串末尾的空格会被删除
print(random_string.rstrip())

# 'si oo' 不是尾随字符,因此不会删除任何内容
print(random_string.rstrip('si oo'))

# 在 'sid oo' 中 'd oo' 是尾随字符,'ood' 从字符串中删除
print(random_string.rstrip('sid oo'))

# 'm/' 是尾随字符,没有找到 '.' 号的尾随字符, 'm/' 从字符串中删除
website = 'www.codercto.com/'
print(website.rstrip('m/.'))


# 移除逗号(,)、点号(.)、字母 s、q 或 w,这几个都是尾随字符
txt = "banana,,,,,ssqqqww....."
x = txt.rstrip(",.qsw")
print(x)
# 删除尾随字符 *
str = "*****this is string example....wow!!!*****"
print (str.rstrip('*'))

以上实例输出结果如下:

this is good
this is good
this is g
www.codercto.co
banana
*****this is string example....wow!!!

查看更多 Python 字符串内建函数

猜你喜欢:
暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册