问Python如何删除字符串末尾的指定字符?

答 1 个回答|241 次阅读
Coco老师 - 官方公众号:青少儿编程学习网
擅长:编程教育网站:https://kidscodes.cn/

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编程学习总结

    Python最适合青少儿进阶学习的编程语言