[Tutor] How does this work?

Kent Johnson kent37 at tds.net
Tue Mar 14 03:48:11 CET 2006


Christopher Spears wrote:
> 
> if p[start:indexpepper].strip():
>                 continue
> 
> What is that supposed to accomplish?  If the program
> can remove whitespace between red and pepper, it's
> supposed to move on?

Not quite - p[start:indexpepper] is a string. 
p[start:indexpepper].strip() is the string with whitespace removed from 
both ends. The test is true if the string is not empty. So essentially 
this is testing to see if p[start:indexpepper] contains anything other 
than whitespace; if so, it the 'continue' is executed which skips the 
rest of that loop iteration and starts the next.
> 
> p = p[:start] + 'bell' + p[start+lenword:]

This builds a new string from pieces of the old one
p[:start] - all of p up to but not including the index start
p[start+lenword:] - all of p from the index start+lenword to the end

> paragraphs[par_no] = p # Change mutable argument!

and inserts it into the list of paragraphs (replacing what was there)

Kent



More information about the Tutor mailing list