[Tutor] updating a print statement

Alan Gauld alan.gauld at btinternet.com
Mon Dec 10 20:09:02 CET 2007

"Bryan Fodness" <bryan.fodness at gmail.com> wrote

> for line in file(file):
>    the_line = line.split()
>    if the_line:
>        print ("Index = %.2f") %index
> Is there a way that only one line will be output and the variable is 
> updated
> rather than one line for every index.

If you use Linux then the curses module will let you address
a specific screen location. If on Windows its a little harder but
you can try using Ctrl-H to delete a character (or several)
before printing.

Use a comma to prevent Python outputting a new line.
Something like::

print '\n\nThe indices are:   ',
for index in range(5):
   print chr(8) * 4,              # ^H *4 => delete four characters
   print ("%2s" % index),    # string formatting forces width to two

There is also a Python WConIO(?) library you can download which gives
cursor addressing under Windows.


Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list