What is the best way to remove all characters from a string that are not in the alphabet? I mean, remove all spaces, interpunction, brackets, numbers, mathematical operators..
For example:
input: 'as32{ vd"s k!+' output: 'asvdsk'asked Dec 11, 2015 at 0:16
0
You could use re, but you don't really need to.
>>> s = 'as32{ vd"s k!+' >>> ''.join(x for x in s if x.isalpha()) 'asvdsk' >>> filter(str.isalpha, s) # works in python-2.7 'asvdsk' >>> ''.join(filter(str.isalpha, s)) # works in python3 'asvdsk'answered Dec 11, 2015 at 0:19
timgebtimgeb
74.5k20 gold badges114 silver badges139 bronze badges
If you want to use regular expression, This should be quicker
import re s = 'as32{ vd"s k!+' print re.sub('[^a-zA-Z]+', '', s)prints 'asvdsk'
answered Dec 11, 2015 at 0:26
nehemnehem
11.5k6 gold badges54 silver badges78 bronze badges
Here is a method that uses ASCII ranges to check whether an character is in the upper/lower case alphabet (and appends it to a string if it is):
s = 'as32{ vd"s k!+' sfiltered = '' for char in s: if((ord(char) >= 97 and ord(char) <= 122) or (ord(char) >= 65 and ord(char) <= 90)): sfiltered += charThe variable sfiltered will show the result, which is 'asvdsk' as expected.
answered Dec 11, 2015 at 0:36
Patrick YuPatrick Yu
9421 gold badge7 silver badges19 bronze badges
This simple expression get all letters, including non ASCII letters ok t áàãéèêçĉ... and many more used in several languages.
r"[^\W\d]+"It means "get a sequence of one or more characters that are not either "non word characters" or a digit.
answered Apr 28 at 3:16
plpsanchezplpsanchez
1853 silver badges10 bronze badges
If you'd like to preserve characters like áàãéèêçĉ that are used in many languages around thw world, try this:
import re print re.sub('[\W\d_]+', yourString)answered Jun 2 at 14:36
In this tutorial, we will look at how to keep only letters (extract alphabets) from a string in Python with the help of examples.
How to extract only alphabets from a string in Python?
You can use a regular expression to extract only letters (alphabets) from a string in Python. You can also iterate over the characters in a string and using the string isalpha() function to keep only letters in a string.
Let’s look at both the methods with the help of examples –
Extract alphabets from a string using regex
You can use the regular expression 'r[^a-zA-Z]' to match with non-alphabet characters in the string and replace them with an empty string using the re.sub() function. The resulting string will contain only letters.
Let’s look at an example.
import re # string with letters, numbers, and special characters s = "[email protected]" # keep only letters res = re.sub(r'[^a-zA-Z]', '', s) print(res)Output:
BuckyBarnesYou can see that the resulting string contains only letters.
Using string isalpha() function
Alternatively, you can use the string isalpha() function to remove non-alphabet characters from the string. Use the following steps –
- Create an empty string to store our result string with only letters.
- Iterate through each character in our given string.
- For each character, check if its an alphabet using the string isalpha() function. If it is, then add the character to our result string.
Let’s look at an example.
# string with letters, numbers, and special characters s = "[email protected]" # keep only letters res = "" for ch in s: if ch.isalpha(): res += ch print(res)Output:
BuckyBarnesThe result string contains only letters from the original string.
The above code can be reduced to fewer lines using list comprehension.
# string with letters, numbers, and special characters s = "[email protected]" # keep only letters res = "".join([ch for ch in s if ch.isalpha()]) print(res)Output:
BuckyBarnesWe get the same result as above.
You might also be interested in –
- Python – Check If String Contains Only Letters
- Python – Remove Non Alphanumeric Characters from String
- Remove Substring From a String in Python
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any
time.
Piyush is a data scientist passionate about using data to understand things better and make informed decisions. In the past, he's worked as a Data Scientist for ZS and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.
View all posts