'<char> in <string>' works, why doesnt '<string> in <string>'?
morton at dennisinter.com
Sat Mar 9 17:40:50 CET 2002
Ok, I see how this works.
'<string1> in <string2>' would always returns false; as the elements
of a <string2> are all characters you can never find <string1> amongst
them (unless its a string of length 1).
Its counter-intuitive for someone playing with strings as strings
rather than as sequences of characters, however.
'c' in 'the quick brown fox' -> 1
'fox' in 'the quick brown fox' -> 1 (instead of TypeError)
Is there any circumstance where youd expect (or want) the later not to
work? I expected and wanted it to work (but instead got an error), its
quite a clear and unambiguous expression (for strings only).
Justin Sheehy <justin at iago.org> wrote in message news:<mailman.1015633276.4847.python-list at python.org>...
> morton at dennisinter.com (damien morton) writes:
> >>>> 'c' in 'the quick brown fox'
> >>>> '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.
More information about the Python-list