1. 程式人生 > >python3 去除字串中的數字

python3 去除字串中的數字

來自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'