tjreedy at udel.edu
Sun Feb 18 03:21:08 EST 2001
"Walter Moreira" <walterm at cmat.edu.uy> wrote in message
news:mailman.982374799.24358.python-list at python.org...
> Why the following test raise an error?
> >>> '' in 'yY'
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> TypeError: 'in <string>' requires character as left operand
Precisely for the reason stated. A character is a string of length 1. A
null string (of length 0) is not a character. Neither are strings of
length >= 2 [for which one must use find() or match()].
> I would expect it to give 0, false.
The empty set is contained in every set as a subset. The empty string is a
substring of every string. So, if defined, I would expect 1 = true.
> With the actual behavior, one must write
> resp = raw_input('Yes or no: ')
> if resp and resp in 'yY':
This is a good example of what 'and' is meant for. Note that if '' in
string were defined as true, you would still have to write the above,
presuming that you did not want to interpret it as yes.
> Is this a planned behavior?
Based on the error message, yes.
Terry J. Reedy
More information about the Python-list