How to extract only characters from a given string in Python

In this tutorial, we are going to learn how to extract only the characters from a given string in Python. So let us first clear some basics before diving into the main topic.

String:

The ‘string’ is one of the many data types in python and strings are usually enclosed in ” ” (double quotes)  or  ‘ ‘ (single quotes) in python.

We can format a string as multi-line using triple quotes (‘ ‘ ‘ ___’ ‘ ‘) single triple quotes or double triple quotes (” ” ” ____ ” ” “).  The line breaks, we give as input are reflected in the output exactly the same.

 

Extracting only characters from string in Python:

  • METHOD 1: using ord(char)
#input random string from the user
rand_string= input("Enter a random mix string: ")

#declare a variable to store the char elements
only_char=""

#for loop to check the string for char elements
for char in rand_string:
    
#ord(char) method to check for char elements by their ascii value
   #checking for char elements with upper case 
    if ord(char) >=65 and ord(char) <=90:
        only_char+=char
    #checking for char elements with lower case 
    elif ord(char) >=97 and ord(char)<=122:
        only_char+=char
#the last print statement gives the output having the filtered string with only char elements
print(only_char)

OUTPUT:

Enter a random mix string: This123is456thefiltered789string0
Thisisthefilteredstring

EXPLANATION:

  1. Accept random string as input from a user, declare a variable to store the input.
  2. Using a for loop check the entire string and extract only character elements using the ord(char) function.
  3. ord(char) method is an inbuilt method in python. It checks character elements for their ASCII value.
  4. An empty variable is initialized to store a string with a filtered string having only char elements. This variable is also used to print the output.

 

  • Method 2: using isalpha( ) function

This is another simple method for extracting only character elements from a given string. Following is the code for extracting the character elements from a string using isalpha() function.

#input random string from the user
rand_string= input("Enter a random mix string: ")

#declare a variable to store the char elements
only_char=""

#for loop to check the string for char elements
for char in rand_string:
    #isalpha() method is used here to check for alphabets (characters) in the string
    if char.isalpha(): 
        only_char+=char
#the last print statement gives the output having the filtered string with only char elements
print(only_char)

OUTPUT:

Enter a random mix string: This123is456thefiltered789string0
Thisisthefilteredstring

EXPLANATION:

  1. Accept random string as input from a user, declare a variable to store the input.
  2. Using a for loop check the entire string and extract only character elements using the isalpha() function.
  3. isalpha() method is an inbuilt method in python. It checks for the presence of character elements in a string.
  4. An empty variable is initialized to store a string with a filtered string having only char elements. This variable is also used to print the output.

CONCLUSION:

These were the two simple methods for extracting character elements from a string of any given length.

Also read: Extract numbers from a string in Python

Leave a Reply

Your email address will not be published. Required fields are marked *