Strings comparison

Karl A. Krueger kkrueger at example.edu
Mon Dec 30 13:15:42 EST 2002


Dusausoy Bruno <bruno.dusausoy at skynet.be> wrote:
> TypeError: 'in <string>' requires character as left operand

>        if o in ("-h"):
                 ^^^^^^
>        if o in ("-o"):
                 ^^^^^^

Python thinks these are parenthesized string expressions, and so it is
doing a string "in" operation.  I think you want it to think they are
tuples.  In order to create a tuple of one item, you need to put a comma
after the item, e.g.:  ("-h",)

This is so that Python can also use parentheses for ordinary arithmetic
grouping as well as tuples:  ( 2 + 3 )  evaluates to 5, not to a 1-tuple
containing 5.

-- 
Karl A. Krueger <kkrueger at example.edu>
Woods Hole Oceanographic Institution
Email address is spamtrapped.  s/example/whoi/
"Outlook not so good." -- Magic 8-Ball Software Reviews



More information about the Python-list mailing list