Keep only alphabets in string python

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

Keep only alphabets in string python

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

Keep only alphabets in string python

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 += char

The variable sfiltered will show the result, which is 'asvdsk' as expected.

answered Dec 11, 2015 at 0:36

Keep only alphabets in string python

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?

Keep only alphabets in string 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:

BuckyBarnes

You 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 –

  1. Create an empty string to store our result string with only letters.
  2. Iterate through each character in our given string.
  3. 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:

BuckyBarnes

The 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:

BuckyBarnes

We 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

How do I keep letters only in a string Python?

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.

How do you input only the alphabet in Python?

The a-z and A-Z characters represent lowercase and uppercase ranges of letters..
Use a while loop to iterate until the user enters only letters..
Use the str. isalpha() method to check if the user entered only letters..
If the condition is met, break out of the loop..

How do you filter only the alphabet of a string in Python?

Given Matrix, write a Python program to extract rows which only contains alphabets in its strings..
Examples:.
Method #1: Using isalpha() + all() + list comprehension..
Output:.
Method #2 : Using filter() + lambda + join() + isalpha().
Output:.
Time Complexity: O(n2).
Space Complexity: O(n).

How do you store alphabets in Python?

The easiest way to load a list of all the letters of the alphabet is to use the string. ascii_letters , string. ascii_lowercase , and string. ascii_uppercase instances.