Hello, readers! In this article, we will be focusing on Different Ways of Extracting characters from an alphanumeric Python string in detail.
So, let us get started!
Before diving deep into the techniques, I would want to grab your attention to a simple question that many of our readers may have while they come across this article, that is,
When would you need these techniques to separate the characters from an alphanumeric string?
Hold on! Don’t worry 🙂 Your question would be answered right away..
So, you see, when we get the data for analysis from various sources such as web scraping, surveys, historic entries, etc i.e is when we understand the data stands out to be in a raw format.
Before analysis of data, we need to clean and pre-process it. Thus, we may need these techniques to get only the characters out of an alphanumeric string accordingly.
Now, let us get started with the explanation of the techniques in the below section.
Python provides us with string.isalpha()
method to detect the presence of characters in a alphanumeric string.
The isalpha() method checks for the presence of alphabets(lower as well as upper case) in a string and returns True only if it encounters at least one character i.e. an alphabet.
Syntax:
string.isalpha()
Example:
import re str1 = "7953abcd[)12!zfee" print ("Original alphanumeric string : ", str1) char = "" for x in str1: if x.isalpha(): char = "".join([char, x]) print ("Extracted characters from the alphanumeric string: ", str(char))
In the above example, we have iterated every character of the alphanumeric string using a for loop. Further, we have checked for the presence of alphabets using isalpha() method. As soon as an alphabet is detected, it gets added to the created string variable ‘char’.
Output:
Original alphanumeric string : 7953abcd[)12!zfee Extracted characters from the alphanumeric string: abcdzfee
Python regex library enables us to detect any kind of customized or defined regular expression from an input string.
Further, we make use of the re.split() function
which splits the string only when it encounters any sort of regular expression pattern passed to it as a parameter.
At last, we add the extracted characters to the string variable using join() method as shown below–
Example:
import re str1 = "7953abcd[)12!zfee" print ("Original alphanumeric string : ", str1) char = "".join(re.split("[^a-zA-Z]*", str1)) print ("Extracted characters from the alphanumeric string: ", str(char))
Output:
Original alphanumeric string : 7953abcd[)12!zfee Extracted characters from the alphanumeric string: abcdzfee
3. Using Python re.findall() function to pull characters from an alphanumeric string
Python re.findall() function enables us to detect all the alphabets from the alphabets from the alphanumeric string.
The re.findall() function
accepts the regular expression(pattern) as well as the string to be searched as input parameters. Then, it returns all the matching patterns from the input string as shown below.
Example:
import re str1 = "7953abcd[)12!zfee" print ("Original alphanumeric string : ", str1) char = "".join(re.findall("[a-zA-Z]+", str1)) print ("Extracted characters from the alphanumeric string: ", str(char))
Output:
Original alphanumeric string : 7953abcd[)12!zfee Extracted characters from the alphanumeric string: abcdzfee
Conclusion
By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question.
For more such posts related to Python, Stay tuned @ Python with JournalDev and till then,
Happy Learning!! 🙂