I would like to make several statements that give standard output without seeing newlines in between statements.
Specifically, suppose I have:
for it
I think a simple join should work:
nl = []
for x in range(1,10):nl.append(str(x))
print ' '.join(nl)
"By the way...... How to refresh it every time so it print mi in one place just change the number."
It's really tricky topic. What zack suggested ( outputting console control codes ) is one way to achieve that.
You can use (n)curses, but that works mainly on *nixes.
On Windows (and here goes interesting part) which is rarely mentioned (I can't understand why) you can use Python bindings to WinAPI (http://sourceforge.net/projects/pywin32/ also with ActivePython by default) - it's not that hard and works well. Here's a small example:
import win32console, time
output_handle = win32console.GetStdHandle( win32console.STD_OUTPUT_HANDLE )
info = output_handle.GetConsoleScreenBufferInfo()
pos = info["CursorPosition"]
for i in "\\|/-\\|/-":
output_handle.WriteConsoleOutputCharacter( i, pos )
time.sleep( 1 )
Or, if you want to use print
(statement or function, no difference):
import win32console, time
output_handle = win32console.GetStdHandle( win32console.STD_OUTPUT_HANDLE )
info = output_handle.GetConsoleScreenBufferInfo()
pos = info["CursorPosition"]
for i in "\\|/-\\|/-":
print i
output_handle.SetConsoleCursorPosition( pos )
time.sleep( 1 )
win32console
module enables you to do many more interesting things with windows console... I'm not a big fan of WinAPI, but recently I realized that at least half of my antipathy towards it was caused by writing WinAPI code in C - pythonic bindings are much easier to use.
All other answers are great and pythonic, of course, but... What if I wanted to print on previous line? Or write multiline text, than clear it and write the same lines again? My solution makes that possible.
Or even simpler:
import time
a = 0
while True:
print (a, end="\r")
a += 1
time.sleep(0.1)
end="\r"
will overwrite from the beginning [0:] of the first print.
So many complicated answers. If you have python 3, simply put \r
at the start of the print, and add end='', flush=True
to it:
import time
for i in range(10):
print(f'\r{i} foo bar', end='', flush=True)
time.sleep(0.5)
This will write 0 foo bar
, then 1 foo bar
etc, in-place.
Use print item,
to make the print statement omit the newline.
In Python 3, it's print(item, end=" ")
.
If you want every number to display in the same place, use for example (Python 2.7):
to = 20
digits = len(str(to - 1))
delete = "\b" * (digits + 1)
for i in range(to):
print "{0}{1:{2}}".format(delete, i, digits),
In Python 3, it's a bit more complicated; here you need to flush sys.stdout
or it won't print anything until after the loop has finished:
import sys
to = 20
digits = len(str(to - 1))
delete = "\b" * (digits)
for i in range(to):
print("{0}{1:{2}}".format(delete, i, digits), end="")
sys.stdout.flush()
for item in range(1,100):
if item==99:
print(item,end='')
else:
print (item,end=',')
Output: 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99