I have this example string: happy t00 go 129.129
and I want to keep only the spaces and letters. All I have been able to come up with so far that is pretty efficient is:
print(re.sub("\d", "", 'happy t00 go 129.129'.replace('.', '')))
but it is only specific to my example string. How can remove all characters other than letters and spaces?
whitelist = set('abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ')
myStr = "happy t00 go 129.129$%^&*("
answer = ''.join(filter(whitelist.__contains__, myStr))
Output:
>>> answer
'happy t go '
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With