Python | Extract only characters from given string
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
import re
ini_string = "123()#$ABGFDabcjw"
ini_string2 = "abceddfgh"
print ( "initial string : " , ini_string, ini_string2)
res1 = " " .join(re.split( "[^a-zA-Z]*" , ini_string))
res2 = " " .join(re.split( "[^a-zA-Z]*" , ini_string2))
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
import re
ini_string = "123()#$ABGFDabcjw"
ini_string2 = "abceddfgh"
print ( "initial string : \n" , ini_string, "\n" , ini_string2)
res1 = " " .join(re.findall( "[a-zA-Z]+" , ini_string))
res2 = " " .join(re.findall( "[a-zA-Z]+" , ini_string2))
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
ini_string = "123()#$ABGFDabcjw"
print ( "initial string : " , ini_string)
res1 = ""
for i in ini_string:
if i.isalpha():
res1 = "".join([res1, i])
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
ini_string = "123()#$ABGFDabcjw"
lower = "abcdefghijklmnopqrstuvwxyz"
upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
a = lower + upper
print ( "initial string : " , ini_string)
res1 = ""
for i in ini_string:
if i in a:
res1 + = i
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))
|
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))
|
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
def extract_characters(string):
return "".join( filter ( lambda x: x.isalpha(), string))
print (extract_characters( "123()#$ABGFDabcjw" )) # Output: ABGFDabcjw
print (extract_characters( "abceddfgh" ))
|
Output
ABGFDabcjw
abceddfgh
Time complexity: O(n)
Auxiliary Space: O(n)
Method #8:Using itertools.filterfalse() method
Python3
import itertools
def extract_characters(string):
return "".join(itertools.filterfalse( lambda x: not x.isalpha(), string))
print (extract_characters( "123()#$ABGFDabcjw" )) # Output: ABGFDabcjw
print (extract_characters( "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
input_string1 = "123()#$ABGFDabcjw"
input_string2 = "abceddfgh"
translation_table = str .maketrans(
" ", " ", " !@
output_string1 = input_string1.translate(translation_table)
output_string2 = input_string2.translate(translation_table)
print (output_string1)
print (output_string2)
|
Output
ABGFDabcjw
abceddfgh
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
13 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...