![图片[1]-python怎么删除字符串最后一个字符?-uusu优素-乐高,模型,3d打印,编程](http://uusucn.zbbe.cn/wp-content/uploads/2024/01/5ebb53d972da1607.jpg)
python删除字符串最后一个字符的方法:
1、使用strip()方法删除最后一个字符
Python strip() 方法用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列。
strip()方法语法:str.strip([chars]);
参数:chars — 移除字符串头尾指定的字符序列。
示例:
str="0000000thisisstringexample....wow!!!0000000";
printstr.strip('0');
输出结果:
thisisstringexample....wow!!!
2、将字符串转换为列表,然后使用pop()方法删除最后一个字符
a='abcdefg' b=list(a) b.pop() print(b)
输出结果如下:
原文来自:https://www.py.cn['a', 'b', 'c', 'd', ' ', 'e', 'f']
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END

















































暂无评论内容