Some Python 2.1 ideas
pbjorn at uswest.net
Sat Dec 23 17:34:41 EST 2000
string.chomp() (a better name would be nice) needs to handle all of:
- "foo \n"
- "foo \r\n"
- "foo \n\r"
etc. correctly, which means it should delete between zero and two bytes
(characters) from the end of the string. This is very different from
removing leading or trailing whitespace (whitespace can be significant
you know <wink>).
Before someone suggests:
line = line[:-1]
I would note that this removes one character too many if you're reading
a file that doesn't end in a newline.
Pete Shinners wrote:
> "Bob Alexander" <bobalex at home.com> wrote
> > String method to remove trailing newline
> there is a string method named "strip()" that will
> remove all whitespace (newline&linefeeds included)
> from a string.
> actually, since strings are immutable, it returns a
> new string with the appropriate changes made.
> there are also "rstrip" and "lstrip" functions which
> will remove whitespace from only the end or beginning
> of the string.
> >>> print 'Example string gotten from readline\n'.strip()
> 'Example string gotten from readline'
More information about the Python-list