"in" operator for strings

Magnus Lie Hetland mlh at idi.ntnu.no
Thu Feb 1 11:36:24 CET 2001


"Henning VON ROSEN" <hvrosen at world-online.no> wrote in message
news:2jae6.119$o3.4587 at news.world-online.no...
>
> "Pete Shinners" <shredwheat at mediaone.net> skrev i melding
[...]
> > >>> "Waldo" in "Ralph Emerson"
> > 0
> > >>> "Waldo" in "Ralph Waldo Emerson"
> > 1
>

This isn't quite logical... A string works like a sequence
of characters, and sequence membership only works on
single elements (in this case characters), not subsequences
(in this case, substrings).

So you could well say

  "W" in "Ralph Waldo Emerson"

(giving a 1), just as in

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

But you can't do what you ask for, just like you can't write

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

> try this:
> >>> "Waldo" in "Ralph Waldo Emerson".split()
> 1

Probably a better idea. And for those of us who can't really
get used to calling methods directly on string literals,
you *could* write:

   "Waldo" in split("Ralph Waldo Emerson")

It might be old-fashioned, but... So what :-)

>
> as long as you are looking for a "word"

Right.

>
> amike via Henning

Ho - Esperantisto, Cxu? <palpebrumo>

--

  Magnus Lie Hetland      (magnus at hetland dot org)

 "Reality is what refuses to disappear when you stop
  believing in it"                 -- Philip K. Dick






More information about the Python-list mailing list