[Tutor] Printing to Screen

Rodrigues op73418@mail.telepac.pt
Wed Jul 30 16:40:04 2003


-----Original Message-----
From: tutor-admin@python.org [mailto:tutor-admin@python.org]On Behalf
Of DORSEY_EDMUND_K@LILLY.COM
Sent: quarta-feira, 30 de Julho de 2003 21:19
To: tutor@python.org
Subject: [Tutor] Printing to Screen



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

Putting a comma next to what you want to print prevents Python from
adding a newline, e.g.

>>> for i in xrange(5):
... 	print i,
...
0 1 2 3 4

But it does add a space. The best may be to concatenate every string
you want to print, e.g.:

>>> to_print = []
>>> for i in xrange(5):
... 	to_print.append(i)
...
>>> string_to_print = "".join([str(i) for i in to_print])
>>> print string_to_print
01234

It's left as an exercise figuring out the whys and the whats of the
code. If you have any doubts just holler.

With my best regards,
G. Rodrigues