How to I print without newline ?

Michael Geary Mike at DeleteThis.Geary.com
Thu May 27 12:15:52 EDT 2004


> I want to print, but without newline. I want to create a progress for
> ftp, but the print is drop a newline for every percent.
> I want like this:
>
> 0% 25% 50% 75% 100%
>
> But this happening:
> 0%
> 25%
> 50%
> 75%
> 100%
>
> How to I prevent the newlines ?

Since you want spaces between the values, a comma at the end of the print
statement will do the trick:

>>> for p in ( 0, 25, 50, 75, 100 ):
...     print '%s%%' % p,
...
0% 25% 50% 75% 100%
>>>

In the more general case, where you want complete control and want to be
able to prevent this extra space, use sys.stdout.write():

>>> for i in xrange(20):
...     sys.stdout.write( '+' )
...
++++++++++++++++++++>>>

Note that there wasn't a newline at the end. You'll need to do a
sys.stdout.write('\n') when you want one.

-Mike





More information about the Python-list mailing list