python怎么删除字符串最后一个字符?
发布时间:2026-04-07 15:33:08

删除python字符串最后一个字符的方法:
1、使用strip()删除最后一个字符
Python strip() 该方法用于删除字符串头尾指定的字符(默认为空格或换行符)或字符序列。
strip()方法语法:str.strip([chars]);
参数:chars -- 删除字符串头尾指定的字符序列。
示例:
str="00000thisstringexample...wow!!!0000000";
printstr.strip('0');
输出结果:
thisisstringexample...wow!!!2、将字符串转换成列表,然后使用pop()删除最后一个字符的方法
a='abcdefg' b=list(a) b.pop() print(b)
输出结果如下:
['a', 'b', 'c', 'd', ' ', 'e', 'f']
请关注Python视频教程栏目,了解更多Python知识。
下一篇 python怎么设置计时器
