How can I check if a string contains ANY letters from the alphabet?


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

Where string_1 would return False for having no letters of the alphabet in it and string_2 would return True for having letter.

Asked By: Justin Papez



How about:

>>> string_1 = "(555).555-5555"
>>> string_2 = "(555) 555 - 5555 ext. 5555"
>>> any(c.isalpha() for c in string_1)
>>> any(c.isalpha() for c in string_2)
Answered By: DSM

Regex should be a fast approach:'[a-zA-Z]', the_string)
Answered By: JBernardo

You can use regular expression like this:

import re

Answered By: shibly

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

>>> z = "(555) 555 - 5555 ext. 5555"
>>> z.isupper() or z.islower()

below: no letters in the string: test yields false.

>>> z= "(555).555-5555"
>>> z.isupper() or z.islower()

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:

>>> text = "(555).555-5555 extA. 5555"
>>> text.islower()
>>> text.isupper()

The better approach is to first upper- or lowercase your string and then check.

>>> string1 = "(555).555-5555 extA. 5555"
>>> string2 = '555 (234) - 123.32   21'

>>> string1.upper().isupper()
>>> string2.upper().isupper()
Answered By: Barm

You can also do this in addition

import re
val ='[a-zA-Z]+',string) 
val[0].isalpha() # returns True if the variable is an alphabet
print(val[0]) # 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

Answered By: Ronald Saunfe

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

import re

~3 µs for'[a-zA-Z]', string)

~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 and it doesn’t even require any additional imports.

Answered By: Mihir Verma
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.