How to remove all special characters from a string except spaces in Python

1 Answer

0 votes
import re

s = '!Beam- me& ~up, Scotty;.'

s = re.sub(r"[^a-zA-Z0-9]", " ", s)
s = " ".join(s.split())

print(s)


    
 
'''
run:
 
Beam me up Scotty

'''

 



answered Apr 14, 2021 by avibootz
...