newbie ``print`` question

gwhite gwhite at ti.com
Sun Sep 2 16:58:25 EDT 2012


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

Given the input of several posters, I feel like I should probably be
using the `write` function anyway.   (I don't have a problem pre-
constructing strings either.)

But I am a newbie, and "all" the show-and-tell examples/tutorials use
`print`.  But this little thread has helped me a lot.



More information about the Python-list mailing list