Check if all characters of a string are uppercase

ffgghhffgghh picture ffgghhffgghh · Nov 24, 2015 · Viewed 55.4k times · Source

Say I have a string that can contain different characters:

e.g. word = "UPPER£CASe"

How would I test the string to see if all the characters are uppercase and no other punctuation, numbers, lowercase letters etc?

Answer

Yash Mehrotra picture Yash Mehrotra · Nov 24, 2015

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.