helix
helix

Reputation: 1056

python - printing items inline in IPython

I need to print everything inline dynamically in a for loop in an IPython notebook. I am using python 2.7. I used the code:

for i in xrange(10):
   print "." , 
   #some computation and no print statement

and even used:

for i in xrange(10):
   print "\b."
   #some computation and no print statement

but both of the above solutions doesn't seem to work in IPython notebooks.

what I need is: .......... and what the above snippets are printing is:

.
.
.
.
.
.
.
.
.
.

Any and every help is appreciated.

Upvotes: 0

Views: 1515

Answers (4)

formatkaka
formatkaka

Reputation: 1358

You can do it using sys module.

import sys

for i in xrange(10):
    sys.stdout.write(".")

The print function is a wrapper which converts the object to string and formats the output.

Upvotes: 0

aldanor
aldanor

Reputation: 3481

This will work on both Python 2 and Python 3:

from __future__ import print_function

for i in range(10):
    print('.', end='')

You can also pass flush=True keyword argument to force it to flush the buffer instantly.

You can also overwrite a previous line using '\r', her's a simple example:

import time

for i in range(10):
    time.sleep(0.2)
    print('\r{} / 10'.format(i + 1, 10), end='')

Upvotes: 4

Mr.F
Mr.F

Reputation: 936

how about

from __future__ import print_function
for i in xrange(10):
   print(".", end=' ')

Upvotes: 0

Mikanchu
Mikanchu

Reputation: 1

I may not understand your question but have you tried to use:

print "."*10

You would get:

..........

Upvotes: -2

Related Questions