Reputation: 91
Goal : i want only alphabets to be printed in a string
#Input
#======
string = ' 529Wind3@. '
#Neededoutput
#============
'Wind'
I tried coding for this using the below code
import re
string=re.sub('[^a-z]+[^A-Z]',' ',string)
print(string)
The output i'm getting is
ind
But this code only applies for lowercase Can you please tell me how to write code for both upper and lowercase
Upvotes: 1
Views: 71
Reputation: 1439
You need to write [^a-zA-Z]
instead of [^a-z]+[^A-Z]
. The +
operator is for detecting repetitive characters and not to combine multiple conditions.
Try the below code for your requirement:
import re
string=re.sub('[^a-zA-Z]',' ',string)
print(string)
Upvotes: 1
Reputation: 16952
I agree with @U8-Forward's point but I think you may also want to know why your regular expression isn't working. This
[^a-z]+[^A-Z]
doesn't do what you want because W
matches [^a-z]+
and so gets removed.
Put all of the characters you don't want in a single character class:
[^a-zA-Z]+
Upvotes: 2
Reputation: 71580
Try using a list comprehension to check if each character is in string.ascii_letters
or not, if it is, it will be stored:
import string
String = ' 529Wind3@. '
print(''.join([i for i in String if i in string.ascii_letters]))
Output:
Wind
Upvotes: 1
Reputation:
you can use re.findall
import re
String = ' 529Wind3@. '
string = re.findall('[a-zA-Z]+', String)
print(''.join(string))
Upvotes: 1