473,324 Members | 2,456 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

print ending with comma

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?

Jul 21 '05 #1
2 5097
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:
print "+",;print "-",;sys.stdout.write("=");print "X" + -= X

Or try this to suppress unwanted spaces in your output:
def nospace(s): sys.stdout.softspace = 0
return s
print "a",nospace("b"),"c"

ab c

Jul 21 '05 #2
ja****************@gmail.com wrote:
[snip]

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.
import sys
print 'a', a print 'b', b

---
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é

Jul 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Karl Scalet | last post by:
Hi, quite often there is a need to just print out the items of a list. that would be nice, except prt() does not exist, and print is a statement not a function, hence cannot replace prt as...
30
by: Martin Bless | last post by:
Why can't we have an additional 'print' statement, that behaves exactly like 'print' but doesn't insert that damn blank between two arguments? Could be called 'printn' or 'prin1' or 'prinn'...
9
by: Paul Watson | last post by:
I thought that using a comma at the end of a print statement would suppress printing of a newline. Am I misunderstanding this feature? How can I use print and not have a newline appended at the...
14
by: Marcin Ciura | last post by:
Here is a pre-PEP about print that I wrote recently. Please let me know what is the community's opinion on it. Cheers, Marcin PEP: XXX Title: Print Without Intervening Space Version:...
0
by: bearophileHUGS | last post by:
There is/was a long discussion about the replacement for print in Python 3.0 (I don't know if this discussion is finished): http://mail.python.org/pipermail/python-dev/2005-September/055968.html ...
9
by: TY | last post by:
Hi all, I have this little simple script: for i in range(10): for j in range(5000000): pass # Timing-delay loop print i When you run it, it behaves as you would expect -- it prints 0...
3
by: Michael Yanowitz | last post by:
Hello: I am still relatively new to Python. I am confused by the syntax for tuples. I had: thread.start_new_thread(read_data_thread, (strDataFilename)) and got back the following error: ...
69
by: Edward K Ream | last post by:
The pros and cons of making 'print' a function in Python 3.x are well discussed at: http://mail.python.org/pipermail/python-dev/2005-September/056154.html Alas, it appears that the effect of...
2
by: John [H2O] | last post by:
Just a quick question.. what do I need to do so that my print statements are caught by nohup?? Yes, I should probably be 'logging'... but hey.. Thanks! -- View this message in context:...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.