[Python-au] strip function

Matthew Schinckel matt@null.net
Tue, 28 Jan 2003 18:00:18 +1030


on 28/1/03 5:45 PM, Andrew Bennetts wrote:

> On Tue, Jan 28, 2003 at 05:27:31PM +1100, vector180 wrote:
>> Having som eproblems with the strip function.
>> Activestate Pythonwin.
>> some doc seems to indicate you can set which characters you want striped
>> from the ends.
>> strip([chars])
>> i was hopeing strip("h") would strip leading,trailing  h s for eg.
>> I have tried various configurations i hope I just have the syntax wrong.
> 
> If I recall correctly, this was introduced in Python 2.2... which version of
> Python do you have?

Maybe try giving us some actual code you are using - this gives me the
correct result:

>>> st = "hello there"
>>> st.strip("h")
'ello there'


Maybe you are expecting it to strip 'in-place' like list.sort() does?

Strings, like tuples are immutable - they cannot be changed.  To get
effectively in-place stripping, try:

>>> st = "hello there"
>>> st = st.strip("h")
>>> st
'ello there'

Of course, if you are using an older version of python (before the great
types/classes re-unification) you will need to:

import string
st = "hello world"
st = string.strip(st, "h")

Good luck.

Matt.
-- 
Matthew Schinckel <matt@null.net>


  It's actually a corruption of "starling".  They used to be carried.
  Since they weighed a full pound (hence the name), they had to be
  carried by two starlings in tandem, with a line between them.
                 -- Alan J Rosenthal explains "Pounds Sterling" on asr