[Tutor] creating the equivalent of string.strip()

Ricardo Aráoz ricaraoz at gmail.com
Wed Oct 3 12:49:07 CEST 2007


Alan Gauld wrote:
> "Ricardo Aráoz" <ricaraoz at gmail.com> wrote
> 
>> 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)
> 
> It still won't work. Strings are immutable, you can't use del
> to delete a character. Try it.

It comes straight from Idle (still warm ;-) ). Notice s = list(s) and s
= ''.join(s), so I'm not using del with strings but with a list and that
is ok. Try it, it does work.

> 
>>>> del('foo'[-1])
> Traceback (most recent call last):
>   File "<input>", line 1, in ?
> TypeError: object doesn't support item deletion
> 

Not what I'm doing.

Cheers

Ricardo



More information about the Tutor mailing list