Check if all characters of a string are uppercase Check if all characters of a string are uppercase python python

Check if all characters of a string are uppercase


You should use str.isupper() and str.isalpha() function.

Eg.

is_all_uppercase = word.isupper() and word.isalpha()

According to the docs:

S.isupper() -> bool

Return True if all cased characters in S are uppercase and there is at least one cased character in S, False otherwise.


You could use regular expressions:

all_uppercase = bool(re.match(r'[A-Z]+$', word))


Yash Mehrotra has the best answer for that problem, but if you'd also like to know how to check that without the methods, for purely educational reasons:

import stringdef is_all_uppercase(a_str):    for c in a_str:        if c not in string.ascii_uppercase:            return False    return True