Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to replace punctuation with space python

I use this code for removing punctuation from sentence but Now I need to replace the punctuation with space

sentence = 'hi,how are you?'
temp = ''.join([''.join(c for c in s if c not in string.punctuation) for s in sentence])

outPut => `hihow are you`

I need to be like this

outPut => `hi how are you`

I need the fastest way to do that

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can tweak your generator comprehension, by using conditional expression:

import string

sentence = 'hi,how are you?'
temp = ''.join(c if c not in string.punctuation else ' ' for c in sentence)
print(temp) # hi how are you
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading