[Tutor] print function (again)

Erik Johnson ejohnso9@earthlink.net
Sun, 21 Jul 2002 12:15:57 -0600


This is a multi-part message in MIME format.

------=_NextPart_000_000A_01C230B0.5E2E5FB0
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

On Wed, July 17 Guillermo Fernandez was asking about how to print =
without the automatic newline appended:

> I would like to do something like:
>
> print "first fct"
> fct1()
> print " second fct"
> fct2()
> print " third fct"
> fct3()
>
> and have an output like:
> "first fct second fct third fct"


Steve (lonetwin@yahoo.com) responded by saying:

 You need to append a "," at the end for your string, thats all.
ie:
 print "first fct",=20
 fct1()
 print "second fct",
 .....
 .....

will print
 first fct second fct ....
	The comma will aslo take care of adding the space between strings that =
get=20
printed continuously.



    I, too, had the same question as Guillermo and thought I would share =
what I have figured out. The automatic newline is often appropriate and =
therefore often handy, but depending on what you are trying to do, it =
can equally as well be really obnoxious. If you intend to build up a =
string that is going to be separated by spaces then Steve's solution is =
dandy, but if you don't want spaces between what you are outputting in =
separate function calls, then "that simply don't work".

For example:

for x in range(5):
  print x,

0 1 2 3 4


It does NOT print: "01234"

to get the string above, one solution is to call stdout's write() method =
directly:

import sys
for x in range(5):
    sys.stdout.write(x)

01234


to make this a little more convenient, you could always do something of =
this flavor:

>>> def printf(s):          # note that this does not handle variable =
argument lists
        sys.stdout.write(s)

>>> printf
<function printf at 0x00A4F6D0>
>>> for x in range(5):
        printf(x)

=20
01234
>>>=20

    An alternate approach is to simply build up a big string as you go, =
having your functions return strings rather than printing anything and =
then concatenating those together with whatever else you want to print =
and dumping it all at once. This may or may not be convenient.

    I think it is somewhat unfortunate that Python did not adopt a =
Pascal-like approach to this function such as print() giving "normal" =
print behaviour and println() for when you want an extra "\n" on the =
end. I am a PyNewbie and know that much of Python's internals are =
exposed, but I don't readily see a way to hack "print" because it seems =
to be handled differently (keyword?) than other functions. Contrast the =
following:

>>> type(sys.stdout.write)
<type 'instance method'>
>>> sys.stdout.write
<bound method PseudoFile.write of <PyShell.PseudoFile instance at =
0x009F7168>>
>>> type(print)
SyntaxError: invalid syntax
>>> print

>>>=20


    Anyway, I hope that this helps and perhaps more advanced PyHackers =
would care to comment on subverting the print function for one's own =
devices. :)

-ej

------=_NextPart_000_000A_01C230B0.5E2E5FB0
Content-Type: text/html;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.2716.2200" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3D"Courier New" size=3D2>On Wed, July 17 Guillermo =
Fernandez was=20
asking about how to print without the automatic newline =
appended:</FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3D"Courier New" size=3D2>&gt;</FONT><FONT =
face=3D"Courier New"><FONT=20
size=3D2><I> I would like to do something=20
like:<BR></I>&gt;</FONT></FONT><I><BR></I><FONT face=3D"Courier New"=20
size=3D2>&gt;</FONT><FONT face=3D"Courier New"><FONT size=3D2><I> print =
"first=20
fct"<BR></I>&gt;</FONT></FONT><FONT face=3D"Courier New"><FONT =
size=3D2><I>=20
fct1()<BR></I>&gt;</FONT></FONT><FONT face=3D"Courier New"><FONT =
size=3D2><I> print=20
" second fct"<BR></I>&gt;</FONT></FONT><FONT face=3D"Courier New"><FONT =
size=3D2><I>=20
fct2()<BR></I>&gt;</FONT></FONT><FONT face=3D"Courier New"><FONT =
size=3D2><I> print=20
" third fct"<BR></I>&gt;</FONT></FONT><FONT face=3D"Courier New"><FONT =
size=3D2><I>=20
fct3()<BR></I>&gt;</FONT></FONT><I><BR></I><FONT face=3D"Courier New"=20
size=3D2>&gt;</FONT><FONT face=3D"Courier New"><FONT size=3D2><I> and =
have an output=20
like:<BR></I>&gt;</FONT></FONT><I><FONT face=3D"Courier New" size=3D2> =
"first fct=20
second fct third fct"<BR></FONT></I></DIV>
<DIV><I><FONT face=3D"Courier New" size=3D2></FONT></I>&nbsp;</DIV>
<DIV><FONT face=3D"Courier New" size=3D2>Steve (</FONT><A=20
href=3D"mailto:lonetwin@yahoo.com"><FONT face=3D"Courier New"=20
size=3D2>lonetwin@yahoo.com</FONT></A><FONT face=3D"Courier New" =
size=3D2>) responded=20
by saying:</FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3D"Courier New" size=3D2>&nbsp;You need to append a "," =
at the end=20
for your string, thats all.<BR>ie:<BR> print "first fct", <BR> =
fct1()<BR> print=20
"second fct",<BR> .....<BR> .....<BR><BR>will print<BR> first fct second =
fct=20
....<BR>	The comma will aslo take care of adding the space between =
strings that=20
get <BR>printed continuously.<BR></FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3D"Courier New" size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3D"Courier New" size=3D2>&nbsp;&nbsp;&nbsp; I, too, had =
the same=20
question as Guillermo and thought I would share what I have figured out. =
The=20
automatic newline is often appropriate and therefore often handy, but =
depending=20
on what you are trying to do, it can equally as well be really =
obnoxious. If you=20
intend to build up a string that is going to be separated by spaces then =
Steve's=20
solution is dandy, but if you don't want spaces between what you are =
outputting=20
in separate function calls, then "that&nbsp;simply don't =
work".</FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3D"Courier New" size=3D2>For example:</FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3D"Courier New" size=3D2>for x in range(5):<BR>&nbsp; =
print=20
x,</FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3D"Courier New" size=3D2>0 1 2 3 4</FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3D"Courier New" size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3D"Courier New" size=3D2>It does NOT print: =
"01234"</FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3D"Courier New" size=3D2>to get the string above, one =
solution is to=20
call stdout's write() method directly:</FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3D"Courier New" size=3D2>import sys</FONT></DIV>
<DIV>
<DIV><FONT face=3D"Courier New" size=3D2>for x in =
range(5):<BR>&nbsp;&nbsp;&nbsp;=20
sys.stdout.write(x)</FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3D"Courier New" size=3D2>01234</FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3D"Courier New" size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3D"Courier New" size=3D2>to make this a little more =
convenient, you=20
could always&nbsp;do something of this flavor:</FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3D"Courier New" size=3D2>&gt;&gt;&gt; def=20
printf(s):&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # note =
that=20
this does not handle variable argument lists</FONT></DIV>
<DIV><FONT face=3D"Courier New" =
size=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
sys.stdout.write(s)</FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3D"Courier New" size=3D2>&gt;&gt;&gt; =
printf<BR>&lt;function printf=20
at 0x00A4F6D0&gt;<BR>&gt;&gt;&gt; for x in=20
range(5):<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
printf(x)</FONT></DIV>
<DIV><FONT face=3D"Courier New"></FONT>&nbsp;</DIV>
<DIV><FONT face=3D"Courier New" size=3D2>&nbsp;<BR>01234<BR>&gt;&gt;&gt; =

</FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3D"Courier New" size=3D2>&nbsp;&nbsp;&nbsp; An alternate =
approach is=20
to simply build up a big string as you go, having your functions return =
strings=20
rather than printing anything and&nbsp;then concatenating those together =
with=20
whatever else you want to print and dumping it all at once. This may or =
may not=20
be convenient.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV></DIV>
<DIV><FONT face=3D"Courier New" size=3D2>&nbsp;&nbsp;&nbsp; I think it =
is somewhat=20
unfortunate that Python did not adopt a Pascal-like approach to this =
function=20
such as print() giving "normal" print behaviour and println() for when =
you want=20
an extra "\n" on the end. I am a PyNewbie and know that much of Python's =

internals are exposed, but I don't readily see a way to hack "print" =
because it=20
seems to be handled differently (keyword?) than other functions. =
Contrast the=20
following:</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3D"Courier New" size=3D2>&gt;&gt;&gt;=20
type(sys.stdout.write)<BR>&lt;type 'instance method'&gt;<BR>&gt;&gt;&gt; =

sys.stdout.write<BR>&lt;bound method PseudoFile.write of =
&lt;PyShell.PseudoFile=20
instance at 0x009F7168&gt;&gt;<BR>&gt;&gt;&gt; =
type(print)<BR>SyntaxError:=20
invalid syntax<BR>&gt;&gt;&gt; print</FONT></DIV>
<DIV><FONT face=3D"Courier New"></FONT>&nbsp;</DIV>
<DIV><FONT face=3D"Courier New" size=3D2>&gt;&gt;&gt; </FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3D"Courier New" size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3D"Courier New" size=3D2>&nbsp;&nbsp;&nbsp; Anyway, I =
hope that this=20
helps and perhaps more advanced PyHackers&nbsp;would care to comment on=20
subverting the print function for one's own devices. :)</FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3D"Courier New" size=3D2>-ej</FONT></DIV></BODY></HTML>

------=_NextPart_000_000A_01C230B0.5E2E5FB0--