print ending with comma

J

jamesthiele.usenet

I recently ran into the issue with 'print' were, as it says on the web
page called "Python Gotchas"
(http://www.ferg.org/projects/python_gotchas.html):

The Python Language Reference Manual says, about the print statement,

A "\n" character is written at the end, unless the print statement ends
with a comma.

What it doesn't say is that if the print statement does end with a
comma, a trailing space is printed.
--
But this isn't exactly correct either. If you run this program:
import sys
print '+',
print '-',
sys.stdout.write('=')
print
--
the output is:
+ -=
Note that there is no space after the '-'. (Tested on Win 2000 python
2.3.4, OS X 10.3.9 python 2.3 & 2.4)

I know that this is not a massively important issue, but can someone
explain what's going on?
 
D

Duncan Booth

wrote:
I recently ran into the issue with 'print' were, as it says on the web
page called "Python Gotchas"
(http://www.ferg.org/projects/python_gotchas.html):

The Python Language Reference Manual says, about the print statement,

A "\n" character is written at the end, unless the print statement ends
with a comma.

What it doesn't say is that if the print statement does end with a
comma, a trailing space is printed.
--
But this isn't exactly correct either. If you run this program:
import sys
print '+',
print '-',
sys.stdout.write('=')
print
--
the output is:
+ -=
Note that there is no space after the '-'. (Tested on Win 2000 python
2.3.4, OS X 10.3.9 python 2.3 & 2.4)

I know that this is not a massively important issue, but can someone
explain what's going on?
The space isn't appended to the value printed, it is output before the next
value is printed.

The file object has an attribute softspace (initially 0). If this is 0 then
printing a value simply writes the value to the file. If it is 1 then
printing a value writes a space followed by the value.

After any value which ends with a newline character is printed the
softspace attribute is reset to 0 otherwise it is set to 1. Also when a
print statement ends without a trailing comma it outputs a newline and
resets softspace.

Change your print test a little to see this:
+ -= X

Or try this to suppress unwanted spaces in your output:
sys.stdout.softspace = 0
return s
ab c
 
?

=?ISO-8859-1?Q?Andr=E9_Roberge?=

A "\n" character is written at the end, unless the print statement ends
with a comma.

What it doesn't say is that if the print statement does end with a
comma, a trailing space is printed.
--
But this isn't exactly correct either. If you run this program:
import sys
print '+',
print '-',
sys.stdout.write('=')
print
--
the output is:
+ -= [snip]
I know that this is not a massively important issue, but can someone
explain what's going on?
Actually, it is not a trailing space but a leading space
that is stored and displayed when print is called next.---
sys.stdout.write() does not include such a leading space.

Time to consult python.org about the print statement.:
[http://www.python.org/doc/2.0.1/ref/print.html]

....A space is written before each object is (converted and) written,
unless the output system believes it is positioned at the beginning of a
line...

Yep, another case of RTM :)

André
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,261
Messages
2,571,308
Members
47,976
Latest member
AlanaKeech

Latest Threads

Top