It's because when the rhs is a string, 'in' tests for a substring rather than simple containment. E.g. "ab" in "abc" gives True. So here 'in' is not a collection membership, it only operates on two strings.

On Tue, Apr 3, 2018 at 4:34 PM, Ethan Furman <> wrote:
This behavior was recently brought to my attention [1]:

--> 1 in 'hello'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'in <string>' requires string as left operand, not int

However, in any other collection (set, dict, list, tuple, etc), the answer would be False.

Does anyone remember the reason why an exception is raised in the string instance instead of returning False?


Python-Dev mailing list

--Guido van Rossum (