python - How can I check if a string only contains uppercase or lowercase letters? -


return true if , if there @ least 1 alphabetic character in s , alphabetic characters in s either uppercase or lowercase.

def upper_lower(s):     """ (str) -> bool    >>> upper_lower('abc') true >>> upper_lower('abcxyz') false >>> upper_lower('xyz') true """ 

use re.match

if re.match(r'(?:[a-z]+|[a-z]+)$', s):     print("true") else:     print("nah") 

we don't need add start of line anchor since re.match tries match beginning of string.

so enters in if block if input string contains lowercase letters or uppercase letters.


Comments