See re.sub
, for performance consider a re.compile
to optimize the pattern once.
Below you find a short version which matches all characters not in the range from A
to Z
and replaces them with the empty string. The re.I
flag ignores the case, thus also lowercase [a-z
] characters are replaced.
import re
def charFilter[myString]
return re.sub['[^A-Z]+', '', myString, 0, re.I]
If you really need that loop there are many awnsers, explaining that specifically. However you might want to give a reason why you need a loop.
If you want to operate on the number sequences and thats the reason for the loop consider replacing the replacement string parameter with a function like:
import re
def numberPrinter[matchString] {
print[matchString]
return ''
}
def charFilter[myString]
return re.sub['[^A-Z]+', '', myString, 0, re.I]
In this tutorial, we will look at how to keep only letters [extract alphabets] from a string in Python with the help of examples.
How to extract only alphabets from a string in Python?
You can use a regular expression to extract only letters [alphabets] from a string in Python. You can also iterate over the characters in a string and using the string isalpha[]
function to keep only letters in a string.
Let’s look at both the methods with the help of examples –
Extract alphabets from a string using regex
You can use the regular expression 'r[^a-zA-Z]'
to match with non-alphabet characters in the string and replace them with an empty string using the re.sub[]
function. The resulting string will contain only letters.
Let’s look at an example.
import re # string with letters, numbers, and special characters s = "[email protected]" # keep only letters res = re.sub[r'[^a-zA-Z]', '', s] print[res]
Output:
BuckyBarnes
You can see that the resulting string contains only letters.
Using string isalpha[]
function
Alternatively, you can use the string isalpha[]
function to remove non-alphabet characters from the string. Use the following steps –
- Create an empty string to store our result string with only letters.
- Iterate through each character in our given string.
- For each character, check if its an alphabet using the string
isalpha[]
function. If it is, then add the character to our result string.
Let’s look at an example.
# string with letters, numbers, and special characters s = "[email protected]" # keep only letters res = "" for ch in s: if ch.isalpha[]: res += ch print[res]
Output:
BuckyBarnes
The result string contains only letters from the original string.
The above code can be reduced to fewer lines using list comprehension.
# string with letters, numbers, and special characters s = "[email protected]" # keep only letters res = "".join[[ch for ch in s if ch.isalpha[]]] print[res]
Output:
BuckyBarnes
We get the same result as above.
You might also be interested in –
- Python – Check If String Contains Only Letters
- Python – Remove Non Alphanumeric Characters from String
- Remove Substring From a String in Python
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any
time.
Piyush is a data scientist passionate about using data to understand things better and make informed decisions. In the past, he's worked as a Data Scientist for ZS and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.
View all posts
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
Sometimes, while working with Python lists, we can have a problem in which we need to extract only those strings which contains only alphabets and discard those which include digits. This has application in day-day programming and web development domain. Lets discuss certain ways in which this task can be performed. Method #1 : Using isalpha[] + list comprehension The combination of above functions can be used to perform this task. In this, we extract the string which are alphabets only using isalpha[] and compile whole logic using list comprehension.
Python3
test_list
=
[
'gfg'
,
'is23'
,
'best'
,
'for2'
,
'geeks'
]
print
[
"The original list is : "
+
str
[test_list]]
res
=
[sub
for
sub
in
test_list
if
sub.isalpha[]]
print
[
"Strings after filtering : "
+
str
[res]]
Output :
The original list is : ['gfg', 'is23', 'best', 'for2', 'geeks'] Strings after filtering : ['gfg', 'best', 'geeks']
Method #2 : Using filter[] + lambda The combination of above methods can be used to perform this task. In this, we perform filtering using filter[] and logic for extension to all strings is done using lambda.
Python3
test_list
=
[
'gfg'
,
'is23'
,
'best'
,
'for2'
,
'geeks'
]
print
[
"The original list is : "
+
str
[test_list]]
res
=
list
[
filter
[
lambda
sub: sub.isalpha[], test_list]]
print
[
"Strings after filtering : "
+
str
[res]]
Output :
The original list is : ['gfg', 'is23', 'best', 'for2', 'geeks'] Strings after filtering : ['gfg', 'best', 'geeks']
The Time and Space Complexity for all the methods are the same:
Time Complexity: O[n]
Auxiliary Space: O[n]