[Tutor] creating the equivalent of string.strip()
alan.gauld at btinternet.com
Wed Oct 3 01:29:07 CEST 2007
"Ricardo Aráoz" <ricaraoz at gmail.com> wrote
> So as not to copy strings many times :
> while s.isspace() :
> while s[-1].isspace() :
> del s[-1]
> del s
Sorry, it won;t work. Strings are immutable.
However you can keep a track of the indexes of the
first and last non space characers and use slicing
if the copying seems to be a problem...
first,last = 0,-1
first += 1
last -= 1
There might be an off-by-one error in the slice, its untested...
More information about the Tutor