[Tutor] really basic - finding multiline chunk within larger chunk

Peter Otten __peter__ at web.de
Tue Feb 16 14:10:49 EST 2016


bruce wrote:

> I've got a test, where I have a chunk of text "a" and a subset of text
> "s2a". the subset is multiline.
> 
> For some reason, can't seem to return true on the find. I've pasted in
> http://fpaste.org/323521/, but the following is an example as well.
> (not sure if the psuedo code listed actually shows the chunks of text
> with the whitespace correctly.
> 
> Obviously, this is an "example" not meant to be real code!!
> 
> Thoughts on what I've screwed up?

str.find() returns -1 when the substring is not found. Other return values 
including 0 mean that the substring was found -- the return value is the 
start index of the substring in the larger string:

>>> "foo bar".find("bar")
4
>>> "foo bar".find("foo")
0
>>> "foo bar".find("baz")
-1

If you only want to know if the substring occurs and do not need the start 
index the clearest test uses the in operator:

>>> "foo" in "foo bar"
True




More information about the Tutor mailing list