品易云推流 关闭
文章详情页
文章 > Python常见问题 > python怎么删除字符串最后一个字符?

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

头像

yang

2020-05-13 10:49:1334708浏览 · 0收藏 · 0评论

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视频教程栏目。

关注

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

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

底部广告图