Open In App

Remove character in a String except Alphabet - Python

Last Updated : 18 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Sometimes, we may need to remove characters from a string except for alphabets. For example, given a string s, the task is to remove all characters except for the alphabetic ones (a-z, A-Z). Another example is if the input string is "Hello123!@#" ,the output should be "Hello". Let's explore different methods to remove non-alphabet characters from a string in Python.

Using regular expressions

Most efficient way to remove non-alphabet characters is by using regular expressions. re.sub() Function can replace any character that is not an alphabet with an empty string.

Python
import re
s = "Hello! @World123"# Input string
res = re.sub(r'[^a-zA-Z]', '', s) # Remove all non-alphabet characters
print(res) 

Output
HelloWorld

Explanation:

  • regular expression [^a-zA-Z] matches any character that is not between a-z or A-Z.
  • re.sub() replaces those characters with an empty string, effectively removing them.

Let's explore some more ways and see how we can remove characters in a string except alphabet.

Using string comprehension

We can use string comprehension to iterate through the string and filter out any non-alphabet characters.

Python
s = "Hello! @World123"

# Use string comprehension to filter out non-alphabet characters
res = ''.join([c for c in s if c.isalpha()])
print(res)

Output
HelloWorld

Explanation:

  • We iterate through each character of the string and check if it is alphabetic using the isalpha() method.
  • We only add characters that are alphabetic to the result string.

Using filter() with isalpha()

filter() function can be used in combination with the isalpha() method to filter out non-alphabet characters.

Python
s = "Hello! @World123"

# Filter the string and retain only alphabet characters
res = ''.join(filter(str.isalpha, s))
print(res)  

Output
HelloWorld

Explanation:

  • The filter() function filters the characters based on the condition str.isalpha(), which checks if the character is alphabetic.
  • The join() method then combines the filtered characters into a single string.

Using for loop

We can also use a simple for loop to manually build a new string by appending only alphabetic characters.

Python
s = "Hello! @World123"

# Initialize an empty result string
res = ""

# Iterate through each character in the string
for c in s:
    if c.isalpha():
        res += c
print(res)  

Output
HelloWorld

Explanation:

  • We initialize an empty string res.
  • We loop through each character in the string and append it to res if it is alphabetic using the isalpha() method.

Using translate()

translate() method can be used in combination with str.maketrans() to remove characters that are not alphabetic.

Python
s = "Hello! @World123"

# Initialize an empty result string
res = ""

# Iterate through each character in the string
for c in s:
    if c.isalpha():
        res += c
print(res)  

Output
HelloWorld

Explanation:

  • str.maketrans() creates a translation table where we specify the characters to be removed.
  • The translate() method uses this table to remove any unwanted characters from the string.

Practice Tags :

Similar Reads