Python program to remove all character from string except alphabets

Remove all character from a string except alphabets

In this article we will learn about how to Remove all character from a string except alphabets

We will do this by checking ASCII values of each character present in the string and remove all character that does not lie in the range of alphabetic ASCII value whether it is an uppercase letter or a lowercase letter.

Remove all character from a string except alphabets in python

Algorithm

  • Step 1:- Start.
  • Step 2:- Take user input.
  • Step 3:- Initialize a empty string.
  • Step 4:- Start iterating through string.
  • Step 5:- Check for the ASCII value of character and whether it lie in the range.
  • Step 6:- If TRUE concatenate characters to empty string.
  • Step 7:- Print String2.
  • Step 8:- End.

Python program to remove all characters from string except alphabets

Run
#take user input
String1 = "#Justice!For@Chutki123"
#initialize empty String
String2 = ''
for i in String1:
    #check for alphabets
    if (ord(i) >= 65 and ord(i) <= 90) or (ord(i) >= 97 and ord(i) <= 122):
        #concatenate to empty string
        String2+=i
print('Alphabets in string are :' + String2)
Alphabets in string are :JusticeForChutki

Mehod 2

Run
def removeSpecialCharacter(s):
    i = 0
    while i < len(s):
        # Finding the character whose ASCII value fall under this range
        if (ord(s[i]) < ord('A') or ord(s[i]) > ord('Z') and ord(s[i]) < ord('a') or ord(s[i]) > ord('z')):
            # erase function to remove the character
            del s[i]
            i -= 1
        i += 1
    print("".join(s))

# main Code
if __name__ == '__main__':
    s = "P*r;e..pi, n'st^a?"
    s = [i for i in s]
    removeSpecialCharacter(s)

Output

Prepinsta

Method 3

Here we are keeping track of two indexes.

Run
def removeSpecialCharacter(s):
t = ""
for i in s:

# Store only valid characters
if (i >= 'A' and i <= 'Z') or (i >= 'a' and i <= 'z'):
t += i
print(t)

# Driver code
s = "$Pre*p;i..ns, '^ta?"
removeSpecialCharacter(s)

Output

Prepinsta

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

18 comments on “Python program to remove all character from string except alphabets”


  • Amith

    s=input()
    k=””
    for i in s:
    if (ord(i)>64 and ord(i)96 and ord(i) <123):
    k=k+i
    print(k)


  • Ujjawal

    a=input()
    arr=[]
    for i in a:
    if i.isalpha():
    arr.append(i)
    for i in arr:
    print(i,end=”)


  • Prithuraj

    n = input(“Enter the string “)
    x = “”
    for i in n:
    if i.isalpha():
    x += i
    else:
    pass
    print(x)


  • 44_Harsh

    spchar = “!@#$%^&*()_+:><{'}?:*/12&^%$#@3%45~6~:;9&^$@0-+”
    string = “T**HA24#$@%N&^k Y(*&^%O*&^%U <{'}?:*/12&^%$#@3%45~6~:;9&^$@0-+”
    string = “T**HA24#$@%N&^k Y(*&^%O*&^%U <P%$#RE*&^P 8675Insta"
    for i in string:
    if i not in spchar:
    print(i,end="")
    THANk YOU PREP Insta


  • Afaque

    st=input(“Enter the string : “)
    st1=”
    for i in st:
    if i.isalpha():
    st1+=i
    print(st1)


  • Yashi

    string = input()
    string1=str()
    for i in string:
    if i.isalpha():
    string1=string1+i
    print(string1)


  • Sankalp

    Another approach using regex:-
    import re
    s=”#Justice!For@Chutki123″
    pattern=”[a-zA-Z ]”
    newstr=””
    for i in s:
    if (re.findall(pattern, i)):
    newstr+=i
    print(newstr)