Alan Gauld wrote:
> "Ricardo Aráoz" <[EMAIL PROTECTED]> wrote
>
>> So as not to copy strings many times :
>>
>> while s[0].isspace() :
>> while s[-1].isspace() :
>> del s[-1]
>> del s[0]
>
> 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
>
> while s[first].isspace():
> first += 1
> while s[last].isspace()
> last -= 1
> return s[first:last]
>
> There might be an off-by-one error in the slice, its untested...
>
> Alan g.
>
sorry, forgot a piece of the code :
s = list(s)
while s[0].isspace() :
while s[-1].isspace() :
del s[-1]
del s[0]
s = ''.join(s)
You only copy the string twice.
_______________________________________________
Tutor maillist - [email protected]
http://mail.python.org/mailman/listinfo/tutor