[Tutor] script error question [checking for substrings]
alan.gauld@bt.com
alan.gauld@bt.com
Tue Dec 17 07:24:02 2002
> 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
I meant to add to my last message that the find() solution is
not without problems too. If you want to find a "word" rather
than a partial word the splity() method works better:
>>> "Hell" in "Hello from Hell".split()
1
>>> "Hello from Hell".find("Hell") != -1
1
>>> "Hell" in "Hello from Heaven".split()
0
>>> "Hello from Heaven".find("Hell") != -1
1
Of course for this type of search a regular expression is
probably better still!
Alan g.