[Tutor] Printing to Screen

Bob Gailer bgailer@alum.rpi.edu
Wed Jul 30 16:45:01 2003


--=======690D411B=======
Content-Type: multipart/alternative; x-avg-checked=avg-ok-6B4038B; boundary="=====================_19877051==.ALT"


--=====================_19877051==.ALT
Content-Type: text/plain; x-avg-checked=avg-ok-6B4038B; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 8bit

At 03:18 PM 7/30/2003 -0500, DORSEY_EDMUND_K@LILLY.COM wrote:

>Is there a way to print but not have it do a new line
>For instance in java you have print and println in C you have to specify 
>the end of line marker.
>Whenever I use print in python is puts a newline.
>Do I need to use a different method for printing to the screen?
>For example...
>
>for x in xrange(0, 5):
>         print x
>
>would print
>
>0
>1
>2
>3
>4
>
>What if I want it to print
>
>01234

There is no direct answer. If you say
     print x,
you will get
0 1 2 3 4
If you print a backspace before each number
     print '\x08%x' % i,
you will get (the desired)
01234
unless you are in pythonwin which prints a graphic interpretation of 
control characters.

The better approach is to collect the desired output in a string, then 
print the string
result = ''
for x in xrange(0, 5):
     result += str(x)
print x

Or create, then join, a list:
print ''.join([str(x) for x in xrange(0,5)])

Bob Gailer
bgailer@alum.rpi.edu
303 442 2625


--=====================_19877051==.ALT
Content-Type: text/html; x-avg-checked=avg-ok-6B4038B; charset=us-ascii
Content-Transfer-Encoding: 8bit

<html>
<body>
At 03:18 PM 7/30/2003 -0500, DORSEY_EDMUND_K@LILLY.COM wrote:<br><br>
<blockquote type=cite class=cite cite><font size=2>Is there a way to
print but not have it do a new line</font> <br>
<font size=2>For instance in java you have print and println in C you
have to specify the end of line marker.</font> <br>
<font size=2>Whenever I use print in python is puts a newline.
</font><br>
<font size=2>Do I need to use a different method for printing to the
screen? </font><br>
<font size=2>For example...</font> <br><br>
<font size=2>for x in xrange(0, 5):</font> <br>
<font size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print x</font>
<br><br>
<font size=2>would print</font> <br><br>
<font size=2>0</font> <br>
<font size=2>1</font> <br>
<font size=2>2</font> <br>
<font size=2>3</font> <br>
<font size=2>4</font> <br><br>
<font size=2>What if I want it to print </font><br><br>
<font size=2>01234</font> </blockquote><br>
There is no direct answer. If you say <br>
&nbsp;&nbsp;&nbsp; <font size=2>print x</font>, <br>
you will get<br>
0 1 2 3 4<br>
If you print a backspace before each number<br>
&nbsp;&nbsp;&nbsp; print '\x08%x' % i,<br>
you will get (the desired)<br>
01234<br>
unless you are in pythonwin which prints a graphic interpretation of control characters.<br><br>
The better approach is to collect the desired output in a string, then print the string<br>
result = ''<br>
<font size=2>for x in xrange(0, 5):</font> <br>
&nbsp;&nbsp;&nbsp; result += str(x)<br>
print x <br><br>
Or create, then join, a list:<br>
print ''.join([str(x) for x in xrange(0,5)])<br>
<x-sigsep><p></x-sigsep>
Bob Gailer<br>
bgailer@alum.rpi.edu<br>
303 442 2625<br>
</body>
</html>


--=====================_19877051==.ALT--

--=======690D411B=======
Content-Type: text/plain; charset=us-ascii; x-avg=cert; x-avg-checked=avg-ok-6B4038B
Content-Disposition: inline


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.500 / Virus Database: 298 - Release Date: 7/10/2003

--=======690D411B=======--