Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

string

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.

like image 454
Justin Papez Avatar asked Jan 31 '12 00:01

Justin Papez


People also ask

How do you check if a string contains any alphabet in C?

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.

How do you check if a string contains any letters Python?

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).


2 Answers

Regex should be a fast approach:

re.search('[a-zA-Z]', the_string) 
like image 57
JBernardo Avatar answered Nov 04 '22 17:11

JBernardo


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 
like image 42
DSM Avatar answered Nov 04 '22 15:11

DSM