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.
C isalpha() In C programming, isalpha() function checks whether a character is an alphabet (a to z and A-Z) or not. If a character passed to isalpha() is an alphabet, it returns a non-zero integer, if not it returns 0. The isalpha() function is defined in <ctype. h> header file.
Python String isalpha() method is a built-in method used for string handling. The isalpha() methods returns “True” if all characters in the string are alphabets, Otherwise, It returns “False”. This function is used to check if the argument includes only alphabet characters (mentioned below).
Regex should be a fast approach:
re.search('[a-zA-Z]', the_string)
How about:
>>> string_1 = "(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) True
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With