What is best pure Python implementation to check if a string contains ANY letters from the alphabet?
string_1 = "(555).555-5555" string_2 = "(555) 555 - 5555 ext. 5555
string_1 would return
False for having no letters of the alphabet in it and
string_2 would return
True for having letter.
Regex should be a fast approach:
"(555).555-5555" string_2 = "(555) 555 - 5555 ext. 5555" any(c.isalpha() for c in string_1) False any(c.isalpha() for c in string_2) Truestring_1 =
You can use
islower() on your string to see if it contains some lowercase letters (amongst other characters).
or it with
isupper() to also check if contains some uppercase letters:
below: letters in the string: test yields true
"(555) 555 - 5555 ext. 5555" z.isupper() or z.islower() Truez =
below: no letters in the string: test yields false.
"(555).555-5555" z.isupper() or z.islower() Falsez=
Not to be mixed up with
isalpha() which returns
True only if all characters are letters, which isn’t what you want.
Note that Barm’s answer completes mine nicely, since mine doesn’t handle the mixed case well.
I liked the answer provided by @jean-françois-fabre, but it is incomplete.
His approach will work, but only if the text contains purely lower- or uppercase letters:
"(555).555-5555 extA. 5555" text.islower() False text.isupper() Falsetext =
The better approach is to first upper- or lowercase your string and then check.
"(555).555-5555 extA. 5555" string2 = '555 (234) - 123.32 21' string1.upper().isupper() True string2.upper().isupper() Falsestring1 =
You can use regular expression like this:
import re print re.search('[a-zA-Z]+',string)
I tested each of the above methods for finding if any alphabets are contained in a given string and found out average processing time per string on a standard computer.
~250 ns for
~3 µs for
~6 µs for
any(c.isalpha() for c in string)
~850 ns for
Opposite to as alleged, importing re takes negligible time, and searching with re takes just about half time as compared to iterating isalpha() even for a relatively small string.
Hence for larger strings and greater counts, re would be significantly more efficient.
But converting string to a case and checking case (i.e. any of upper().isupper() or lower().islower() ) wins here. In every loop it is significantly faster than re.search() and it doesn’t even require any additional imports.
You can also do this in addition
import re string='24234ww' val = re.search('[a-zA-Z]+',string) val.isalpha() # returns True if the variable is an alphabet print(val) # this will print the first instance of the matching value
Also note that if variable val returns None. That means the search did not find a match