当前位置: 首页 > 图灵资讯 > 行业资讯> python字符串不要最后字符怎么做?

python字符串不要最后字符怎么做?

来源:图灵python
时间: 2026-04-27 15:48:37

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

rstrip()方法语法:str.rstrip([chars])

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

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

示例:

#!/usr/bin/python3

S="thisisstringexample...wow!!!"
print(S.rstrip())
S="*****thisisstringexample...wow!!!*****"
print(S.rstrip('*'))

输出结果如下:

thisisstring example...wow!!!

*****thisisstring example...wow!!!

请关注Python视频教程栏目,了解更多Python知识。