newbie ``print`` question

MRAB python at mrabarnett.plus.com
Sun Sep 2 20:12:33 EDT 2012


On 03/09/2012 00:33, gwhite wrote:
> On Sep 2, 3:43 pm, MRAB <pyt... at mrabarnett.plus.com> wrote:
>> On 02/09/2012 21:58, gwhite wrote:
>>
>>
>>
>>
>>
>>
>>
>> > On Sep 2, 1:16 pm, Dave Angel <d... at davea.name> wrote:
>> >> On 09/02/2012 03:50 PM, gwhite wrote:
>>
>> >> > On Sep 2, 12:43 pm, Dave Angel <d... at davea.name> wrote:
>> >> >> On 09/02/2012 03:34 PM, gwhite wrote:
>>
>> >> >>> <snip>
>> >> >>> btw, I also thought the default "add a CR LF" to the end was odd too.
>> >> >>> But at least that one had a simple way out.
>> >> >> But it (print on Python 2.x) doesn't, unless you're stuck on Windows.
>> >> >> And even then, you can prevent it by using a 'b' in the mode.
>> >> > Yes, I'm using windows.  What is "'b' in the mode?"  The help for
>> >> > print says:
>>
>> >> > A ``'\n'`` character is written at the end, unless the ``print``
>> >> > statement ends with a comma.  This is the only action if the statement
>> >> > contains just the keyword ``print``.
>>
>> >> > So I followed with a comma to stop the default CR LF insertion.
>>
>> >> You're correct;  the best way to suppress the newline at the end of
>> >> print is to use the trailing comma.  But since print is for lines, it
>> >> usually is a good default.  If you don't want to get any extra
>> >> characters, just use write().  It takes a string, and outputs exactly
>> >> what it's given.
>>
>> >> I assumed you were complaining about the conversion of newline to
>> >> carriage-return-newline, which is done by default on Windows, and can be
>> >> suppressed by opening the file with "b" as the mode parameter.
>>
>> > Sorry, I was a little vague on the newline stuff.
>>
>> > In any case, I've learned I should probably avoid the comma, if
>> > looking at 3.x:
>>
>> >>>> from __future__ import print_function
>> >>>> print('a=%.1f,' % 1.0),;print('b=%.1f' % 2.0)
>> > a=1.0,
>> > (None,)
>> > b=2.0
>>
>> Explanation:
>>
>> With 'print' as a function, the first 'print' prints the result of
>> "'a=%.1f,' % 1.0" and then returns None. The trailing comma makes that
>> into a tuple (None,), which is printed by the interactive interpreter
>> as such.
>>
>> In other words:
>>
>>  >>> None,
>> (None,)
>>  >>>
>>
>> The second prints the result of "'b=%.1f' % 2.0" and then returns None.
>> The interactive interpreter, recognising that it's only None, doesn't
>> bother to print it.
>>
>> In other words:
>>
>>  >>> None
>
> Thanks, that makes sense.
>
> It does look like 2.7 & 3.3 don't parse the comma the same way.  Just
> to repeat, since I didn't give the exact same line to both versions of
> Python:
>
> 2.7
>>>> print('a=%.1f,' % 1.0),;print('b=%.1f' % 2.0)
> a=1.0, b=2.0
>
> This has been more informative than I thought it was going to be.
>
Basically:

   In Python 2, 'print' is a statement.
   In Python 3, 'print' is a function.

so they _would_ be parsed differently, because they _are_ different.

However, in later versions of Python 2, such as 2.7, adding "from
__future__ import print_function" will make 'print' a function like in
Python 3. (It has no effect in Python 3 because 'print' is already a
function, of course, so if you've converted a Python 2 script which
contains it to Python 3 and just happened to leave it in, it won't
complain either.)



More information about the Python-list mailing list