How can I remove a trailing newline?

Try the method rstrip() (see doc Python 2 and Python 3) Python’s rstrip() method strips all kinds of trailing whitespace by default, not just one newline as Perl does with chomp. To strip only newlines: In addition to rstrip(), there are also the methods strip() and lstrip(). Here is an example with the three of them:

How can I remove a trailing newline?

Try the method rstrip() (see doc Python 2 and Python 3) Python’s rstrip() method strips all kinds of trailing whitespace by default, not just one newline as Perl does with chomp. To strip only newlines: In addition to rstrip(), there are also the methods strip() and lstrip(). Here is an example with the three of them: