absolute removal of '\n' and the like

Russell Blau russblau at hotmail.com
Fri Feb 10 18:32:52 EST 2006


"S Borg" <spwpreston at gmail.com> wrote in message
news:1139613718.688010.216370 at z14g2000cwz.googlegroups.com...
>  If I have a string, what is the strongest way to assure the
> removal of any line break characters?
>
> Line break characters must always be the last character in a line, so
> would
> this:    str = linestring[:-1]
>
>  work?

Er, yes, if you don't mind (a) mangling any string that *doesn't* have a
newline as the last character, and (b) messing up any subsequent part of
your program that tries to use the built-in str() function (because you just
reassigned that name to something else).

I'd suggest:

    foo = linestring.rstrip("\n")

You can also add to the quoted string any other characters you want to have
stripped; for example,

    foo = linestring.rstrip("\n\r\t")

Or if you want to strip off *all* whitespace characters, just leave it out:

    foo = linestring.rstrip()

Russ







More information about the Python-list mailing list