How to replace all non-alphanumeric characters from a string in Python

1 Answer

0 votes
import re
  
s = 'Python, #Java@ !C ^&CPP 100 (*)PHP.'
  
pattern = r'[^A-Za-z0-9]+'

s = re.sub(pattern, '@', s)
  
print(s)
  
  
  
  
  
'''
run:
  
Python@Java@C@CPP@100@PHP@

'''

 



answered Mar 22, 2023 by avibootz
...