How to replace all the punctuation characters in a string with specific character in Python

2 Answers

0 votes
from string import punctuation 

s = 'python, (java)- +php/:{}nodejs[];.'

replace = '*'
  
for ch in punctuation: 
     s = s.replace(ch, replace) 

print(s)
  
 
        
'''
run:
 
python* *java** *php****nodejs****
        
'''

 



answered Apr 30, 2020 by avibootz
0 votes
import re 

s = 'python, (java)- +php/:{}nodejs[];.'

replace = '*'

s = re.sub(r'[^\w\s]', replace, s) 

print(s)
  
 
        
'''
run:
 
python* *java** *php****nodejs****
        
'''

 



answered Apr 30, 2020 by avibootz
...