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? -- http://mail.python.org/mailman/listinfo/python-list
Re: print ending with comma
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: print ending with comma
[EMAIL PROTECTED] 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é -- http://mail.python.org/mailman/listinfo/python-list