How to remove all characters except letters from a string in Python

3 Answers

0 votes
s = '23#@a!~aswdf,.ieew*&()^sd%##'

result = ''.join(c for c in s if c.isalpha())

print(result)

'''
run:
 
aaswdfieewsd

'''

 



answered Sep 4, 2018 by avibootz
0 votes
s = '23#@a!~aswdf,.ieew*&()^sd%##'

result = ''.join(filter(str.isalpha, s))    

print(result)

'''
run:
 
aaswdfieewsd

'''

 



answered Sep 4, 2018 by avibootz
0 votes
import re

s = '23#@a!~aswdf,.ieew*&()^sd%##'

result = re.sub(r'[^A-Za-z]', '', s)

print(result)

'''
run:
 
aaswdfieewsd

'''

 



answered Sep 4, 2018 by avibootz
...