+3 votes
in Programming Languages by (74.2k points)
I have a list of strings. Some strings have alphabets while some have just numbers.

e.g.

aa= ['123Hello', '456']

How can I check whether or not a string contains alphabets?

1 Answer

+1 vote
by (349k points)
selected by
 
Best answer

There are several ways to check if a string contains any alphabet. You can use regular expression search() function or islower() or isupper() or isalpha() function. Check the following example:

Approach 1

>>> import re
>>> str1='123456Hello'
>>> if (re.search('[a-zA-Z]', str1)):
...     print ('Yes')
... else:
...     print ('No')
...
Yes

Approach 2

First convert string to lowercase/uppercase() and then apply islower()/isupper().

>>> str1.lower().islower()
True
>>> str1.upper().isupper()
True

Approach 3

Check each character in the string.

>>> any(c.isalpha() for c in str1)
True


...