newbie ``print`` question

G

gwhite

I can't figure out how to stop the "add a space at the beginning"
behavior of the print function.
1 2

See the space in between the 1 and the 2 at the output print to the
command console?

The help for print is:

"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."

So it is apparently doing what it is supposed to do.

Is there a way to stop this? Or is there a different function that
will only print what you have in the formatted string?

For example, in MATLAB I only had to do:
12

fprintf works the same way if printing to a file. It only puts in
what you explicitly tell it to put in.
 
M

Mark Lawrence

I can't figure out how to stop the "add a space at the beginning"
behavior of the print function.

1 2

See the space in between the 1 and the 2 at the output print to the
command console?

The help for print is:

"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."

So it is apparently doing what it is supposed to do.

Is there a way to stop this? Or is there a different function that
will only print what you have in the formatted string?

For example, in MATLAB I only had to do:

12

fprintf works the same way if printing to a file. It only puts in
what you explicitly tell it to put in.

I'd settle for print 12, YMMV.
 
J

Joel Goldstick

I can't figure out how to stop the "add a space at the beginning"
behavior of the print function.

1 2

See the space in between the 1 and the 2 at the output print to the
command console?

The help for print is:

"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."

So it is apparently doing what it is supposed to do.

Is there a way to stop this? Or is there a different function that
will only print what you have in the formatted string?

You can do it with string formatting. My example shows 'old style'
string formatting syntax. There is a newer style. You can learn
about both from the online python docs.
 
M

mblume

Am Sun, 02 Sep 2012 10:23:53 -0700 schrieb gwhite:
I can't figure out how to stop the "add a space at the beginning"
behavior of the print function.

1 2

See the space in between the 1 and the 2 at the output print to the
command console?

The help for print is:

"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."

So it is apparently doing what it is supposed to do.

Is there a way to stop this? Or is there a different function that will
only print what you have in the formatted string?

For example, in MATLAB I only had to do:

12

fprintf works the same way if printing to a file. It only puts in
what you explicitly tell it to put in.

import sys

sys.stdout.write("1"); sys.stdout.write("2")


HTH
Martin
 
T

Terry Reedy

I can't figure out how to stop the "add a space at the beginning"
behavior of the print function.

1 2

You have discovered why print is a function in 3.x.12

In 2.6 or 2.7, you can add
from __future__ import print_function
but I recommend using 3.2 or 3.3 unless you have a reason to use older
Python.
 
G

gwhite

If you were to use Python 3.x, yes. Otherwise, no.


Use the .write() method of the sys.stdout file object.http://docs.python.org/library/sys.html#sys.stdout

Alternatively, you can stick with `print` and rework your code so that
it outputs an entire line at a time (thus, there'd only be 1 argument
passed to `print`, so its "spaces between arguments" feature wouldn't
come into play).

Cheers,
Chris

Thanks.
 
G

gwhite

You have discovered why print is a function in 3.x.
 >>> print(1, 2, sep='')
12
 >>> print(1, end=''); print(2, end='')
12

In 2.6 or 2.7, you can add
   from __future__ import print_function
but I recommend using 3.2 or 3.3 unless you have a reason to use older
Python.

Thanks. I would use 3.2 or 3.3, but I am actually using pythonxy,
which hasn't quite gotten there yet, as the bundle of packages has to
all play nice together. Or so I am guessing. I have been considering
the __future__ technique.
 
G

gwhite

If you were to use Python 3.x, yes. Otherwise, no.


Use the .write() method of the sys.stdout file object.http://docs.python.org/library/sys.html#sys.stdout

Alternatively, you can stick with `print` and rework your code so that
it outputs an entire line at a time (thus, there'd only be 1 argument
passed to `print`, so its "spaces between arguments" feature wouldn't
come into play).

On the "rework" thing, yes, I suppose I could construct the line as a
single string prior to print. There would be things like `for`
loops and conditionals to do so. That isn't so unusual.

I was actually doing some simple tests of other things -- you know,
newbie stuff to make sure I understood what the various ops were
doing. I wasn't even thinking about `print` Then I got sidetracked
with this item, which I thought was very odd, since it is "deciding
for you" to stuff a space in.
 
G

gwhite

On the "rework" thing, yes, I suppose I could construct the line as a
single string prior to print.    There would be things like `for`
loops and conditionals to do so.  That isn't so unusual.

I was actually doing some simple tests of other things -- you know,
newbie stuff to make sure I understood what the various ops were
doing.  I wasn't even thinking about `print` Then I got sidetracked
with this item, which I thought was very odd, since it is "deciding
for you" to stuff a space in.

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.
 
D

Dave Angel

<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.
 
G

gwhite

You can do it with string formatting.  My example shows 'old style'
string formatting syntax.  There is a newer style.  You can learn
about both from the online python docs.



12

For "real" stuff, I've been trying to use that. I saw it in Beazley's
latest Essential text.

You're right, I can construct a single string and then write it. In
fact, it looks like it is a must unless using the 3.2/3.3 version of
`print`.
 
G

gwhite

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.
 
G

gwhite

You have discovered why print is a function in 3.x.
 >>> print(1, 2, sep='')
12
 >>> print(1, end=''); print(2, end='')
12

In 2.6 or 2.7, you can add
   from __future__ import print_function
but I recommend using 3.2 or 3.3 unless you have a reason to use older
Python.

It looks like one can avoid pre-construction of the total line with
3.x.
a=1.0,b=2.0

Thanks. It isn't that hard to pre-construct, but regardless, there is
better control in 3.x.
 
D

Dave Angel

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.
 
D

Dennis Lee Bieber

"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."

So it is apparently doing what it is supposed to do.

Is there a way to stop this? Or is there a different function that
will only print what you have in the formatted string?
E:\UserData\Wulfraed\My Documents>python
ActivePython 2.7.2.5 (ActiveState Software Inc.) based on
Python 2.7.2 (default, Jun 24 2011, 12:21:10) [MSC v.1500 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
In Python 2.x, the equivalent of fprint/fprintf is NOT print but
sys.stdout.write/file_object.write

"print" is just a convenience function that is designed to translate
its arguments into a textual representation, and then drop it to the
screen. Part of the convenience is to separate output items by a space
and to emit a newline at the end.

.write(), OTOH, does no formatting -- not even new lines. You have
to provide all conversion to text, and line endings.
 
T

Terry Reedy

On the "rework" thing, yes, I suppose I could construct the line as a
single string prior to print. There would be things like `for`
loops and conditionals to do so. That isn't so unusual.

The usual idiom is to construct a list of pieces and then join with ''.
12

Or map str to a list of objects.
print(''.join(map(str, [1, 2])))
12

You can do either of these in 2.x.
If you use .write, include '\n' at the end of the list (when needed).

Print was designed as a quick and easy way to put lines of text on the
screen. Then people asked for a way to use with with other streams,
hence the >> hack. Then people wanted ways to control the separator and
terminator. As that point, Guido realized that it needed to be a
function, not a statement, with all the options requested.
 
G

gwhite

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:
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.
 
G

gwhite

"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."
So it is apparently doing what it is supposed to do.
Is there a way to stop this?  Or is there a different function that
will only print what you have in the formatted string?

E:\UserData\Wulfraed\My Documents>python
ActivePython 2.7.2.5 (ActiveState Software Inc.) based on
Python 2.7.2 (default, Jun 24 2011, 12:21:10) [MSC v.1500 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
12

        In Python 2.x, the equivalent of fprint/fprintf is NOT print but
sys.stdout.write/file_object.write

        "print" is just a convenience function that is designed to translate
its arguments into a textual representation, and then drop it to the
screen. Part of the convenience is to separate output items by a space
and to emit a newline at the end.

        .write(), OTOH, does no formatting -- not even new lines.You have
to provide all conversion to text, and line endings.

Thanks. I am starting to get it.
 
G

gwhite

On the "rework" thing, yes, I suppose I could construct the line as a
single string prior to print.    There would be things like `for`
loops and conditionals to do so.  That isn't so unusual.

The usual idiom is to construct a list of pieces and then join with ''.

 >>> print(''.join(['1', '2']))
12

Or map str to a list of objects.

 >>> print(''.join(map(str, [1, 2])))
12

You can do either of these in 2.x.
If you use .write, include '\n' at the end of the list (when needed).

Print was designed as a quick and easy way to put lines of text on the
screen. Then people asked for a way to use with with other streams,
hence the >> hack. Then people wanted ways to control the separator and
terminator. As that point, Guido realized that it needed to be a
function, not a statement, with all the options requested.

Thanks again, Terry. There is a lot to the language, I am finding
out. I am a HW engineer, not really a programmer. Python seems a lot
more sophisticated than MATLAB.

I'm kinda thinking `write` is likely to be a little more "stable" than
`print` (if that is the right characterization) when my eventual
switch from 2.7 to 3.x happens. You think?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top