• 技术文章 >常见问题 >Python常见问题

    python怎么删除字符串最后一个字符?

    yangyang2020-05-13 10:49:13原创34197

    python删除字符串最后一个字符的方法:

    1、使用strip()方法删除最后一个字符

    Python strip() 方法用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列。

    strip()方法语法:str.strip([chars]);

    参数:chars -- 移除字符串头尾指定的字符序列。

    示例:

    str = "0000000this is string example....wow!!!0000000";
    print str.strip( '0' );
    
    输出结果:
    this is string example....wow!!!

    2、将字符串转换为列表,然后使用pop()方法删除最后一个字符

    a = 'abcd efg'
    b = list(a) 
    b.pop()
    print(b)

    输出结果如下:

    ['a', 'b', 'c', 'd', ' ', 'e', 'f']

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

    专题推荐:python
    品易云
    上一篇:python怎么设置计时器 下一篇:python怎么删除下载的包

    相关文章推荐

    • python读取文本内容中文乱码怎么解决• python怎样删除字典中的元素• python怎么让代码无效

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网