Problem with the strip string method

Harold Fellermann dadapapa at googlemail.com
Mon Mar 3 16:04:56 CET 2008


> Thanks to all respondents, Steve Holden
> is right, I expected more than I should
> have.

Others have explained why all your examples work as they should.
>From your exmaples, it seems like you would like strip to
remove the leading and trailing characters from EVERY LINE in
your string. This can be done by the simple construct

>>> my_string = '  foo    \n bar   '
>>> '\n'.join(line.strip() for line in my_string.split('\n'))
'foo\nbar'

If you need this construct at several places, define a function

def line_strip(string,sep='\n') :
    return sep.join(line.strip() for line in string.split(sep))

cheers,

- harold -



More information about the Python-list mailing list