[Tutor] script error question [checking for substrings]
alan.gauld@bt.com
alan.gauld@bt.com
Tue Dec 17 07:20:02 2002
> > An alternative is to split the string:
> >
> > >>> "hello" in "hello world".split()
> > 1
>
> But your split() alternative fails miserably if the substring
> you're looking for contains spaces:
>
> >>> "hello world".find("o w")
> 4
> >>> "o w" in "hello world".split()
> 0
>
> Just something to beware of.
Absolutely, it is only a slightly more readable form that
looks more like the "if X in S". I don't expect anyone
to try
if "x o" in "ghfgfhg hjg jhvhnbmvbv hjb "
For that, and any other general search, find() is undoubtedly best.
Alan G.