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


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

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

1 Answer

0 votes
by (27.9k points)

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

Approach 2

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

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

Approach 3

Check each character in the string.

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