Open In App

Python | Extract only characters from given string

Last Updated : 13 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a string, the task is to extract only alphabetical characters from a string. Given below are few methods to solve the given problem.

Method #1: Using re.split 

Python3




# Python code to demonstrate
# to get characters from string
import re
 
# initialising string
ini_string = "123()#$ABGFDabcjw"
ini_string2 = "abceddfgh"
 
# printing strings
print ("initial string : ", ini_string, ini_string2)
 
# code to find characters in string
res1 = " ".join(re.split("[^a-zA-Z]*", ini_string))
res2 = " ".join(re.split("[^a-zA-Z]*", ini_string2))
 
# printing resultant string
print ("first string result: ", str(res1))
print ("second string result: ", str(res2))


Output

initial string :  123()#$ABGFDabcjw abceddfgh
first string result:    A B G F D a b c j w 
second string result:   a b c e d d f g h 

Time complexity: O(n), where n is the length of the input string.
Auxiliary space: O(n), where n is the length of the input string, as the resultant string is stored in a variable.

Method #2: Using re.findall 

Python3




# Python code to demonstrate
# to get characters in string
import re
 
# initialising string
ini_string = "123()#$ABGFDabcjw"
ini_string2 = "abceddfgh"
 
# printing strings
print ("initial string : \n", ini_string, "\n", ini_string2)
 
# code to find characters in string
res1 = " ".join(re.findall("[a-zA-Z]+", ini_string))
res2 = " ".join(re.findall("[a-zA-Z]+", ini_string2))
 
# printing resultant string
print ("first string result: ", str(res1))
print ("second string result: ", str(res2))


Output

initial string : 
 123()#$ABGFDabcjw 
 abceddfgh
first string result:  ABGFDabcjw
second string result:  abceddfgh

Time complexity: O(n), where n is the length of the input string
Auxiliary space: O(n), where n is the length of the input string.

Method #3: Using isalpha() 

Python3




# Python code to demonstrate
# to get characters in a string
# if present
 
# initialising string
ini_string = "123()#$ABGFDabcjw"
 
# printing string and its length
print("initial string : ", ini_string)
 
# code to find characters in string
res1 = ""
for i in ini_string:
    if i.isalpha():
        res1 = "".join([res1, i])
 
 
# printing resultant string
print("first result: ", str(res1))


Output

initial string :  123()#$ABGFDabcjw
first result:  ABGFDabcjw

Time complexity: O(n), where n is the length of the input string ini_string.
Auxiliary space: O(n), where n is the length of the input string ini_string. 

Method #4 : Without any builtin methods

Python3




# Python code to demonstrate
# to get characters in a string
# if present
 
# initialising string
ini_string = "123()#$ABGFDabcjw"
lower="abcdefghijklmnopqrstuvwxyz"
upper="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
a=lower+upper
# printing string and its length
print ("initial string : ", ini_string)
 
# code to find characters in string
res1 = ""
for i in ini_string:
    if i in a:
        res1+=i
 
 
# printing resultant string
print ("first result: ", str(res1))


Output

initial string :  123()#$ABGFDabcjw
first result:  ABGFDabcjw

The time complexity of this program is O(n), where n is the length of the input string ini_string. 

The auxiliary space required by the program is also O(n), since the program creates a new string res1 to store the characters that are present in the input string.

Method 5: Using list comprehension method 

Python3




ini_string = "123()#$ABGFDabcjw"
a="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
res=[i for i in ini_string if i in a]
print("".join(res))


Output

ABGFDabcjw

Method 6: Using enumerate function

Python3




ini_string = "123()#$ABGFDabcjw"
a="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
res=[j for i,j in enumerate(ini_string) if j in a]
print("".join(res))


Output

ABGFDabcjw

Method #7: Using filter and lambda:

The filter function takes a function and a list-like object as input, and returns a list of elements from the input object for which the function returns True. In this case, the function is lambda x: x.isalpha(), which returns True for any alphabetical character and False for any non-alphabetical character. The join method is then used to concatenate the characters into a single string.

Python3




# Python code to demonstrate
# to get characters in a string
# if present
def extract_characters(string):
  #Using filter to get the characters
    return "".join(filter(lambda x: x.isalpha(), string))
 
# Test the function
print(extract_characters("123()#$ABGFDabcjw"))  # Output: ABGFDabcjw
print(extract_characters("abceddfgh"))  # Output: abceddfgh
#This code is contributed by Edula Vinay Kumar Reddy


Output

ABGFDabcjw
abceddfgh

Time complexity: O(n)
Auxiliary Space: O(n)

Method #8:Using itertools.filterfalse() method

Python3




# Python code to demonstrate
# to get characters in a string
# if present
import itertools
def extract_characters(string):
  #Using filter to get the characters
    return "".join(itertools.filterfalse(lambda x: not x.isalpha(), string))
 
# Test the function
print(extract_characters("123()#$ABGFDabcjw"))  # Output: ABGFDabcjw
print(extract_characters("abceddfgh"))  # Output: abceddfgh


Output

ABGFDabcjw
abceddfgh

Time Complexity: O(N)
Auxiliary Space: O(N)

Method #9:Using str.translate() with str.maketrans()

Here is an implementation using str.translate() with str.maketrans() to remove non-alphabetic characters from a given string:

Algorithm:

Initialize the input string to be extracted from.
Create a translation table using str.maketrans() method where the second argument is all the non-alphabetic characters to be removed from the input string.
Use the translate() method to remove the non-alphabetic characters from the input string using the created translation table.
Print the output string.
 

Python3




# Python code to extract only alphabetical characters from a given string using str.translate() with str.maketrans()
 
# initializing input string
input_string1 = "123()#$ABGFDabcjw"
input_string2 = "abceddfgh"
 
# creating translation table to remove non-alphabetic characters
translation_table = str.maketrans(
    "", "", "!@#$%^&*()_-+={}[]|\:;\"'<>,.?/`~1234567890")
 
# removing non-alphabetic characters from input string using translate()
output_string1 = input_string1.translate(translation_table)
 
output_string2 = input_string2.translate(translation_table)
 
# printing output
print(output_string1)
print(output_string2)


Output

ABGFDabcjw
abceddfgh

Time Complexity: O(N)
Auxiliary Space: O(N)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads