Home > Back-end >  How remove all non-alphabet chars excluding white space from string in Python without joining the wo
How remove all non-alphabet chars excluding white space from string in Python without joining the wo

Time:11-11

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

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

Output getting

'IagoingthroughLotsOfpain'

CodePudding user response:

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.

CodePudding user response:

Chack it

import re
pattern = re.compile('[\W_0-9] ')
para =  '''I a going through Lots Of pain
            kcb H in'''
dirty_list = para.split()
clean_list = [pattern.sub('', word) for word in dirty_list]
clean_string = ' '.join(clean_list)
print(clean_string)
  • Related