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

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

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

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

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

示例:

#!/usr/bin/python3

S="thisisstringexample....wow!!!"

print(S.rstrip())

S="*****thisisstringexample....wow!!!*****"

print(S.rstrip('*'))

输出结果如下:

thisisstringexample....wow!!!

*****thisisstringexample....wow!!!

以上内容为大家介绍了python字符串不要最后字符怎么做?希望对大家有所帮助,如果想要了解更多Python相关知识,请关注我们

推荐阅读