OneMoreError
OneMoreError

Reputation: 7728

str.upper().isupper() might be False

I saw this statement in the official Python documentation :

str.upper().isupper() might be False

Can someone please explain ?

Upvotes: 2

Views: 1548

Answers (6)

tamasgal
tamasgal

Reputation: 26269

If the string is number or is made of characters without an uppercase variant (special characters etc.) For example:

>>> '42'.upper().isupper()
False
>>> '-'.upper().isupper()
False

And as expected:

>>> '42a'.upper().isupper()
True

Be carefule, since there is some strange behaviour for many unicode characters (see the answer from thg435: https://stackoverflow.com/a/16495101/531222)

Upvotes: 5

georg
georg

Reputation: 214969

In my opinion, isupper/islower functions are broken in python. First, they use an incorrect definition of "cased". There are lots of symbols in Unicode that have upper/lower case variants, but don't belong to the L category. For example, is clearly uppercase, and does have the lower case equivalent , but "Ⓐ".isupper() is False in python. See this bug report for more details.

Second, there's a logical mistake. isupper is the same as "all cased chars are upper in this string" and if there are no cased chars, it should return True, not False, just like built-in all() does ("all unicorns are red" is true). This way the confusion in question could be easily avoided.

Upvotes: 4

lunixbochs
lunixbochs

Reputation: 22415

More context to their statement:

str.upper().isupper() might be False if s contains uncased characters or if the Unicode category of the resulting character(s) is not “Lu” (Letter, uppercase), but e.g. “Lt” (Letter, titlecase).

An example of an uncased character:

>>> '1'.upper().isupper()
False

Upvotes: 4

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 251001

If string consists of unicode characters that doesn't support uppercase:

>>> "عربية للفوتوشوب".upper().isupper()
False

str.upper makes no sense for numbers:

>>> "3432".upper().isupper()
False

Upvotes: 2

Elior
Elior

Reputation: 3266

Yes, its true in case the string contains digits and other characters that have no upperCase

Upvotes: 1

Thilo
Thilo

Reputation: 262554

There are things that have no upper case (I guess only letters do, and not even in all languages).

So those won't get uppered by upper and then not be isupper.

Upvotes: 1

Related Questions