品易云推流 关闭
文章详情页
文章 > Python常见问题 > python字符串不要最后字符怎么做?

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

头像

yang

2020-05-07 11:00:402438浏览 · 0收藏 · 0评论

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

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

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

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

示例:

#!/usr/bin/python3
 
S = "     this is string example....wow!!!     "
print (S.rstrip())
S = "*****this is string example....wow!!!*****"
print (S.rstrip('*'))

输出结果如下:

     this is string example....wow!!!

*****this is string example....wow!!!

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

关注

关注公众号,随时随地在线学习

本教程部分素材来源于网络,版权问题联系站长!

底部广告图