What is the Python equivalent of Perl\'s chomp
function, which removes the last character of a string if it is a newline?
I might use something like this:
import os
s = s.rstrip(os.linesep)
I think the problem with rstrip("\n")
is that you'll probably want to make sure the line separator is portable. (some antiquated systems are rumored to use "\r\n"
). The other gotcha is that rstrip
will strip out repeated whitespace. Hopefully os.linesep
will contain the right characters. the above works for me.