string - google python class strings2.py exercise E -


what happens @ line ? why -1 ?

if n != -1  

e. not_bad given string, find first appearance of substring 'not' , 'bad'. if 'bad' follows 'not', replace whole 'not'...'bad' substring 'good'. return resulting string. 'this dinner not bad!' yields: dinner good!

def not_bad(s):     n = s.find('not')     b = s.find('bad')     if n != -1 , b != -1 , b > n:         s = s[:n] + 'good' + s[b+3:]     return s 

-1 means substring not found.

from official python documentation:

return lowest index in s substring sub found such sub wholly contained in s[start:end]. return -1 on failure. defaults start , end , interpretation of negative values same slices.


Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -