python 中如何提取字母?

比如
a = 'hello123 world@#$!'

如何得到a1 = ‘helloworld’

?

第1个回答  2013-11-06
>>> a = 'hello123 world@#$!'
>>> a1=''.join([x for x in a if x.isalpha()])
>>> a1
'helloworld'

 简略形式:

>>> a1=''.join(x for x in a if x.isalpha())
>>> a1
'helloworld'

本回答被提问者采纳
第2个回答  2013-11-06
我只想到一个一个的判断,如下。

a='hello123 world@#$!'
a1=''
for c in a:
if c.isalpha():
a1=a1+c
print(a1)
第3个回答  2013-11-06

or usage regex

>>> import re
>>> a = 'hello123 world@#$!'
>>> patt = re.compile(r"[\W\d]+")
>>> patt.sub('', a)
'helloworld'
>>>

第4个回答  2013-11-06
import re
a = 'hello123 wor_ld@#$!'
s = re.sub('[^a-zA-Z]', '', a)
print(s)
第5个回答  2015-09-14
filter(str.isalpha, '213fdgfg45v5eg')
相似回答