Alex Rivera | Logout

Print in one line dynamically

Asked 2010-07-14T19:03:37.337
377

I would like to make several statements that give standard output without seeing newlines in between statements.

Specifically, suppose I have:

for item in range(1,100):
    print item

The result is:

1
2
3
4
.
.
.

How get this to instead look like:

1 2 3 4 5 ...

Even better, is it possible to print the single number over the last number, so only one number is on the screen at a time?

Edit
Report

2 Answers

5

I think a simple join should work:

nl = []
for x in range(1,10):nl.append(str(x))
print ' '.join(nl)

EDIT: agreeing with the comment, a list comprehension is much better as shown here.

print ' '.join(str(x) for x in range(1, 10))

answered 2012-09-20T17:54:21.727
1

A comma at the end of the print statement omits the new line.

for i in xrange(1,100):
  print i,

but this does not overwrite.

answered 2010-07-14T19:08:44.047

Your Answer