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

Erik Max Francis max at alcyone.com
Fri Mar 8 19:14:54 EST 2002


damien morton wrote:

> is this by design?
> 
> is there any reason why '<string> in <string>' shouldnt work?

Yes, it's by design.  The in operator deals with membership, rather than
finding subsequences.  Strings are a degenerate case, since they're
sequences of "characters" only.  For example,

	[1, 2] in [1, 2, 3, 4]

would rightly return false, since [1, 2] is not a member of [1, 2, 3, 4]
(that would be something like [[1, 2], 1, 2, 3, 4]).  Having in deal
with membership _or_ subsequences would lead to ambiguity, and having it
deal with subsequences among strings alone would be a peculiar
exception.

Say what you mean; there's a .find method (or just string.find) for
finding substrings.

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/  \ Laws are silent in time of war.
\__/ Cicero
    Esperanto reference / http://www.alcyone.com/max/lang/esperanto/
 An Esperanto reference for English speakers.



More information about the Python-list mailing list