Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Not x.islower() Versus x.isupper Output Results

16 views
Skip to first unread message

Christopher Reimer

unread,
Apr 29, 2016, 9:15:07 PM4/29/16
to
Greetings,

I was playing around with a piece of code to remove lowercase letters
and leave behind uppercase letters from a string when I got unexpected
results.

string = 'Whiskey Tango Foxtrot'

list(filter((lambda x: not x.islower()), string))

['W', ' ', 'T', ' ', 'F']

Note the space characters in the list.

list(filter((lambda x: x.isupper()), string))

['W', 'T', 'F']

Note that there are no space characters in the list.

Shouldn't the results between 'not x.islower()' and 'x.isupper()' be
identical?

The final form of this code is this:

list(filter(str.isupper, string))

['W', 'T', 'F']

Thank you,

Chris R.



Steven D'Aprano

unread,
Apr 29, 2016, 9:31:36 PM4/29/16
to
On Sat, 30 Apr 2016 11:14 am, Christopher Reimer wrote:

> Shouldn't the results between 'not x.islower()' and 'x.isupper()' be
> identical?

Of course not.

py> "2 #".islower(), "2 #".isupper()
(False, False)

py> not "2 #".islower(), "2 #".isupper()
(True, False)

"Is Lower" versus "Is Upper" is not a dichotomy. There are:

- lowercase characters, like "abc"
- uppercase characters, like "ABC"
- characters which are neither lowercase nor uppercase, like "2 #"

In unicode, there are also:

- titlecase characters, like "DžLjῼ"

If those characters don't show up for you, they are:

U+01C5 LATIN CAPITAL LETTER D WITH SMALL LETTER Z WITH CARON
U+01C8 LATIN CAPITAL LETTER L WITH SMALL LETTER J
U+1FFC GREEK CAPITAL LETTER OMEGA WITH PROSGEGRAMMENI


So not islower() is very different from isupper.




--
Steven

Steven D'Aprano

unread,
Apr 29, 2016, 9:51:27 PM4/29/16
to
On Sat, 30 Apr 2016 11:31 am, Steven D'Aprano wrote:

> In unicode, there are also:
>
> - titlecase characters, like "DžLjῼ"

To be clear, each of those three characters is considered titlecased
individually. The three of them together is not considered a title-cased
string.

"Is Title" is not just for Unicode. In Python 2, strings (ASCII byte
strings) also have an istitle() method:

py> "Dz".istitle()
True
py> "DZ".istitle()
False



--
Steven

0 new messages