English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Python 基础教程

Python 流程控制

Funzione in Python

Tipi di dati in Python

Python 文件操作

Python 对象和类

Python 日期和时间

Python 高级知识

Python 参考手册

Python 字符串 rstrip() 使用方法及示例

Python 字符串方法

rstrip()方法 删除 string 字符串末尾的指定字符(默认为空格)。

rstrip()根据参数(指定要删除的字符集的字符串)从右侧删除字符。

rstrip()的语法为:

string.rstrip([chars])

rstrip()参数

  • chars (可选)-一个字符串,指定要删除的字符集。

如果没有提供chars参数,则从字符串中删除右侧的所有空格。

rstrip()返回值 

rstrip()返回字符串的副本,其中删除了末尾的字符。

chars从字符串的右边删除参数中所有字符的组合,直到第一次不匹配为止。

示例:rstrip()的工作

random_string = ' this is good'
# 移除前导空白
print(random_string.rstrip())
# 参数不包含“ d”
# 不删除任何字符。
print(random_string.rstrip('si oo'))
print(random_string.rstrip('sid oo'))
website = 'it.oldtoolbag.com/'
print(website.rstrip('m/.'))

运行该程序时,输出为:

 this is good
 this is good
 this is g
www.w3codebox.co

Python 字符串方法