String help

Daniel Yoo dyoo at hkn.eecs.berkeley.edu
Wed Jun 23 16:52:26 EDT 2004


Rigga <Rigga at hasnomail.com> wrote:


: I am having problems when it comes to data in the file that wraps
: over two lines i.e.
:
: las -
:        lomas
:
: What i want to be able to do is to some how strip all the spaces from it
: so while it is contained as a variable so it equal 'las - lomas'



Hi Rigga,

Are you stripping as you're reading the file, line by line, or are you
calling strip() at the very end?  Without seeing code, it's a little
hard to tell what you're doing.


: I have tried it using the string.strip(myvariable,"") however that
: doesnt appear to do anything.

Ah, ok.  string.strip() is deprecated, so you probably shouldn't use
it. 

(... And, also, it is being misused.  The delimiter -- the second
argument to string.strip() --- has to be nonempty to have any real
effect.)


Instead, you can just use the strip() method of strings.  For example:

###
>>> msg = "     hello world     "
>>> msg.strip()
'hello world'
>>> msg.lstrip()
'hello world     '
>>> msg.rstrip()
'     hello world'
###


See:

    http://www.python.org/doc/lib/string-methods.html

for a comprehensive list of the methods that a string can support.


Good luck!



More information about the Python-list mailing list