[Tutor] more problems, my final questions

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Wed, 10 Jul 2002 00:36:19 -0700 (PDT)


Oops, I pressed the email send key too quickly before I finished!  Sorry
about that!


> By the way, the statement above translates to:
>
>     "if begin is equal to the string 'Yy', do this:..."
>
> because if two literal strings are together like that, they'll stick
> together.  For example:

Here's that example:

###
>>> "hello" "world"
'helloworld'
###



> I think that you're trying to find if there's a substring of begin within
> that "CustomcustomCUSTOM" string.  If so, there's another way of
> expressing this:
>
> ###
> def substring(word, sentence):
>     return sentence.find(word) != -1
> ###


I'd better test this, or it's just going to bite me!

###
>>> substring('foo', "FooFOOfoo")
1
>>> substring('food', "FooFOOfoo")
0
###

Whew, it looks like it's working.  *grin*


The reason this works is because if the sentence can find() that word
anywhere within it, it'll give us the index position where that word
starts.  But if it can't find the word, find() returns the somewhat
strange value '-1', which we can use to our advantage.


Hope this helps!