How does one keep python from printing newline at program exit?

Alex Martelli aleaxit at
Mon Oct 11 17:54:58 CEST 2004

S. Staats <sstaats at> wrote:

> Good day, everbody.
> Here is a simple program called
> #!/usr/bin/python
> print "No newline, please.",
> # End of program
> Here is what program does:
> prompt> ./
> No newline, please.
> Here is what I want the program to do:
> prompt> ./
> No newline, please.prompt>
> How can I get the python interpreter to not print a newline when it exits?

You can avoid using print -- use sys.output.write instead -- or you can
use sys.output.softspace=False as the last statement in your program.

The reason for the newline is that, when standard output (sys.output) is
closed, it checks if its softspace attribute is True (meaning there is a
pending line not yet terminated) and if so terminates the line.  In
turn, softspace is set by print with a trailing comma, specifically to
avoid erroneously-unterminated lines (and other small anomalies).  So,
if you want something a bit out of the ordinary such as an unterminated
line being output, either you eschew print, which is meant to help in
typical ordinary cases (sys.stdout.write being there for when you need
fine control), or you reset that special attribute to False.


More information about the Python-list mailing list