lists, uppercase?

Joshua Muskovitz joshm at taconic.net
Thu Jan 31 06:25:05 CET 2002


> >> how can i get to know if a string is totally uppercase?
> >
> >>>> 'ABC'.isupper()
> > 1
> >>>> 'abc'.isupper()
> > 0
> >>>> '123'.isupper()
> > 0
>
> Just be aware of one anomaly: ''.isupper() is 0.

...and '123A'.isupper() is 1!

Try this maybe:

>>> [x.isupper() for x in '123 ABC abc']
[0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0]
>>> import operator
>>> reduce(operator.mul, [x.isupper() for x in '123 ABC abc'], 1)
0
>>> reduce(operator.mul, [x.isupper() for x in 'ABC'], 1)
1
>>> reduce(operator.mul, [x.isupper() for x in 'ABC '], 1)
0
>>> reduce(operator.mul, [x.isupper() for x in 'ABC1'], 1)
0

or, for even less readable code:

>>> reduce(lambda x,y: x*y, [x.isupper() for x in stringToTest], 1)

which avoids importing the operator module.

or (very) slightly more readable:

>>> reduce(lambda x, y: x and y, [char.isupper() for char in stringToTest],
1)

--
# Joshua Muskovitz
# joshm at taconic.net
def lyyrs(sig): return '-'.join(sig.split()+["ly y'rs"])
lyyrs('Hire me!  I need the work!')




-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----==  Over 80,000 Newsgroups - 16 Different Servers! =-----



More information about the Python-list mailing list