ITPub博客

首页 > IT职业 > IT图书 > python怎么删除字符串最后一个字符?

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

IT图书 作者:ahcow 时间:2021-09-11 10:17:01 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视频教程栏目。

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/3663/viewspace-2833693/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-30

  • 博文量
    90
  • 访问量
    948