-1

I want to do something like this.

calculating 50%
calculating 60%
calculating 70%

but in a single line. Did hours of googling and couldn't find anything. :)

  • Already in a lot answers. – dsgdfg Aug 24 '15 at 07:48
  • 1
    There totally isn't any question like that on SO http://stackoverflow.com/questions/6169217/replace-console-output-in-python http://stackoverflow.com/questions/4897359/output-to-the-same-line-overwriting-previous-output-python-2-5 – piezol Aug 24 '15 at 07:48

2 Answers2

1

You can do something like this:

print '50%%',
print '\r60%%',
print '\r70%%',

The comma makes sure there is no newline. The \r clears the current line, so that the previous 50% is removed, and overwritten with the 60%. However, because the print is not flushed (like without the comma), it can happen that you will not see some lines printed. For that, you'll need to flush the output, by using this:

import sys
sys.stdout.flush()
Mathias711
  • 6,266
  • 4
  • 34
  • 51
0

It depends whether you want to overwrite the previous thing or to really write all three statements on one line. The latter you can do by

import sys
sys.stdout.write("calculating 50%")
sys.stdout.write("calculating 60%")
sys.stdout.write("calculating 70%")

The former would be probably achieved by terminal escape sequences an would be different for different OSs.

Jiří Kantor
  • 526
  • 1
  • 3
  • 12