[ILUG] Python strip "]" from string

Francis Daly francisdaly at gmail.com
Wed Oct 8 01:33:22 IST 2008


2008/10/7 Justin Kelly <john.kelly at usa.net>:
> I just ran python and did the following:

> -----------------------------------------------------------------------------
> Python 2.5.1 (r251:54863, Jul 31 2008, 23:17:40)
> [GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.

The first suggestion there is useful.

>>>> a = 'hel[lo]'
>>>> a
> 'hel[lo]'
>>>> a.strip("[")
> 'hel[lo]'

<snip>

>>>> b="[hello]"
>>>> b.strip("]")
> '[hello'
>>>> b.strip("[")
> 'hello]'
>>>>
>
> It appears strip only strips the first or last characters if they are the same
> as the one passed in the function.

Yes, that's what strip() does. More or less. (It doesn't limit itself
to one character.)

help("str")

will say exactly what "string".strip() does. I don't know how
discoverable that command is.

It will also show that perhaps what the OP wanted was something like

a.replace("[","")

>>I must be missing something.
>>
>>Thanks,
>>Oliver.

Why did you try strip() and not trim() or delete() or tr-d()?

There's usually a fine manual somewhere to describe what the functions
really do. If there's a broken manual out there, it would be good to
get it fixed.

Good luck,

f



More information about the ILUG mailing list