2025年3月19日 星期三 甲辰(龙)年 月十八 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 编程开发 > Python

python3 去除字符串中的数字

时间:04-13来源:作者:点击数:47

来自https://stackoverflow.com/questions/12851791/removing-numbers-from-string

python3:

  • from string import digits
  • s = 'abc123def456ghi789zero0'
  • remove_digits = str.maketrans('', '', digits)
  • res = s.translate(remove_digits)
  • # 'abcdefghizero'

或者:

  • filter(lambda x: x.isalpha(), "a1a2a3s3d4f5fg6h")

还可以:

  • for i in range(10):
  • a.replace(str(i),'')

python2:

  • from string import digits
  • s = 'abc123def456ghi789zero0'
  • res = s.translate(None, digits)
  • # 'abcdefghizero'
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门