Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

Python program to remove all character from string except alphabets

Remove all character from a string except alphabets

Strings may contain any printable character it can be number or alphabet or can be some special character. In this python program, we will remove all those numbers or special characters from a string and print only alphabetic string. 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.

Python program to remove all character from a string except alphabets

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

#take user input
String1 = input('Enter the String :')
#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)
Output:
Enter the String :#[email protected]
Alphabets in string are :JusticeForChutki