newbie ``print`` question
Dave Angel
d at davea.name
Sun Sep 2 16:15:42 EDT 2012
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.
--
DaveA
More information about the Python-list
mailing list