[Tutor] Nested loops/test conditions

Paul Sidorsky paulsid@shaw.ca
Mon, 11 Feb 2002 16:39:21 -0700

Jeff Shannon wrote:

> > def fix_string(badString):     # need this to strip newlines, etc. from a
> > readline()
> >     goodString = string.split(badString)
> >     goodString = string.join(goodString)
> >     return goodString
> This is a *very* inefficient way to strip newlines.  

It's also not sound in that it won't necessarily preserve the whitespace
in the middle.  Usually this is undesired but there are times when it
can be useful to "normalize" a string, e.g.:

"This	is   an  	ugly string" becomes:
"This is an ugly string"

Of course it would be a bit more compact to use string member functions

def normalize_string(s):
    return " ".join(s.split())

Anyhow, just thought I'd point this out.  Sometimes bits of code that
don't do what they're intended to do end up doing something else that is
quite interesting in and of itself.  :-)

Paul Sidorsky                                          Calgary, Canada
paulsid@shaw.ca                        http://members.shaw.ca/paulsid/