rstrip()
方法返回字符串的副本,其中删除了末尾字符(根据传入的字符串参数)。
示例
title = 'Python Programming '
# remove trailing whitespace from title
result = title.rstrip()
print(result)
# Output: Python Programming
字符串 rstrip() 的语法
rstrip()
的语法是
string.rstrip([chars])
rstrip() 参数
- chars(可选)- 一个字符串,指定要删除的末尾字符集。
rstrip()
根据参数(指定要删除的字符集的字符串)从右侧删除字符。
如果未提供 chars 参数,则从字符串中删除右侧所有空格。
rstrip() 的返回值
rstrip()
返回字符串的副本,其中删除了末尾字符。
从字符串的右侧删除 chars 参数中字符的所有组合,直到第一次不匹配。
示例:rstrip() 的工作原理
random_string = 'this is good '
# Trailing whitespace are removed
print(random_string.rstrip())
# 'si oo' are not trailing characters so nothing is removed
print(random_string.rstrip('si oo'))
# in 'sid oo', 'd oo' are the trailing characters, 'ood' is removed from the string
print(random_string.rstrip('sid oo')) website = 'www.programiz.com/'
print(website.rstrip('m/.'))
输出
this is good this is good this is g www.programiz.co
另请阅读