Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #28325
| Date | 2012-09-02 23:43 +0100 |
|---|---|
| From | MRAB <python@mrabarnett.plus.com> |
| Subject | Re: newbie ``print`` question |
| References | (3 earlier) <23e8e5c4-fb47-4753-b936-e1d546702387@pz10g2000pbb.googlegroups.com> <mailman.105.1346614942.27098.python-list@python.org> <09a119ef-4374-4740-808f-69f8df555f40@f4g2000pbq.googlegroups.com> <mailman.109.1346616968.27098.python-list@python.org> <18639409-7989-454d-91bc-b7b7b7f3528b@s9g2000pbh.googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.119.1346625829.27098.python-list@python.org> (permalink) |
On 02/09/2012 21:58, gwhite wrote:
> On Sep 2, 1:16 pm, Dave Angel <d...@davea.name> wrote:
>> On 09/02/2012 03:50 PM, gwhite wrote:
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> > On Sep 2, 12:43 pm, Dave Angel <d...@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
>>>
> 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.
>
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
newbie ``print`` question gwhite <gwhite@ti.com> - 2012-09-02 10:23 -0700
Re: newbie ``print`` question Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-09-02 18:41 +0100
Re: newbie ``print`` question Joel Goldstick <joel.goldstick@gmail.com> - 2012-09-02 13:44 -0400
Re: newbie ``print`` question gwhite <gwhite@ti.com> - 2012-09-02 12:46 -0700
Re: newbie ``print`` question mblume <foobar@invalid.invalid> - 2012-09-02 17:49 +0000
Re: newbie ``print`` question gwhite <gwhite@ti.com> - 2012-09-02 11:51 -0700
Re: newbie ``print`` question Chris Rebert <clp2@rebertia.com> - 2012-09-02 10:55 -0700
Re: newbie ``print`` question gwhite <gwhite@ti.com> - 2012-09-02 11:51 -0700
Re: newbie ``print`` question gwhite <gwhite@ti.com> - 2012-09-02 12:26 -0700
Re: newbie ``print`` question gwhite <gwhite@ti.com> - 2012-09-02 12:34 -0700
Re: newbie ``print`` question Dave Angel <d@davea.name> - 2012-09-02 15:41 -0400
Re: newbie ``print`` question gwhite <gwhite@ti.com> - 2012-09-02 12:50 -0700
Re: newbie ``print`` question Dave Angel <d@davea.name> - 2012-09-02 16:15 -0400
Re: newbie ``print`` question gwhite <gwhite@ti.com> - 2012-09-02 13:58 -0700
Re: newbie ``print`` question MRAB <python@mrabarnett.plus.com> - 2012-09-02 23:43 +0100
Re: newbie ``print`` question gwhite <gwhite@ti.com> - 2012-09-02 16:33 -0700
Re: newbie ``print`` question MRAB <python@mrabarnett.plus.com> - 2012-09-03 01:12 +0100
Re: newbie ``print`` question Terry Reedy <tjreedy@udel.edu> - 2012-09-02 16:49 -0400
Re: newbie ``print`` question gwhite <gwhite@ti.com> - 2012-09-02 14:18 -0700
Re: newbie ``print`` question Chris Angelico <rosuav@gmail.com> - 2012-09-03 07:47 +1000
Re: newbie ``print`` question gwhite <gwhite@ti.com> - 2012-09-02 16:20 -0700
Re: newbie ``print`` question Chris Angelico <rosuav@gmail.com> - 2012-09-03 09:28 +1000
Re: newbie ``print`` question Terry Reedy <tjreedy@udel.edu> - 2012-09-02 14:33 -0400
Re: newbie ``print`` question gwhite <gwhite@ti.com> - 2012-09-02 12:18 -0700
Re: newbie ``print`` question Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-09-02 16:37 -0400
Re: newbie ``print`` question gwhite <gwhite@ti.com> - 2012-09-02 14:04 -0700
csiph-web