'<char> in <string>' works, why doesnt '<string> in <string>'?

Justin Sheehy justin at iago.org
Fri Mar 8 19:20:15 EST 2002


morton at dennisinter.com (damien  morton) writes:

>>>> 'c' in 'the quick brown fox'
> 1
>>>> 'fox' in 'the quick brown fox'
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> TypeError: 'in <string>' requires character as left operand
>
> is this by design?
>
> is there any reason why '<string> in <string>' shouldnt work?

The "in" operator tests to see if the first operand is an element of
the sequence which is the second element.  A string is a sequence of
characters (which are 1-element strings).  Thus, the only thing which
can be an element of a string is a single character.

-Justin

 





More information about the Python-list mailing list