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 remove all non-alphabet chars excluding white space from string in Python without joining the words

I have a variable named para and I want to remove all the non-alphabet characters excluding whitespace characters. For the following input:

para = "I a, going #?5 1throu$gh Lots Of ]pain
        kcb H in"

required output

para =  "I a going through Lots Of pain
        kcb H in"

Code Tried

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

import re
regex = re.compile('[^a-zA-Z]')
regex.sub('', para)

Output getting

'IagoingthroughLotsOfpain'

>Solution :

import re
regex = re.compile('[^a-zA-Z\s]')
regex.sub('', para)

\s matches any whitespace character (equivalent to [\r\n\t\f\v ]). See regex101.com.

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