How to make a line updated? I want to count the number of http requests sent by the script per minute and output it all on the same line, not every time a new one. For example, if I write print(string) and want to print this string twice, the output will be as follows:

 Requests per minute: 10; Requests per minute: 20; 

And you need to make sure that the number in this line is updated.

1 answer 1

Use the cursor return character (\ b):

 from __future__ import print_function #for python 2.x print ("123\b\b", end="") print ("55", end="") 

It will print 155 as a result, because after 123 the cursor will move 2 positions backwards.

You can use the move the cursor to the beginning of the line if you intend to reprint the entire line as a whole:

 print ("123\r", end="") print ("456", end="") 
  • I get an invalid syntax error in the print('Successful requests: ' + success + '\r', end='') , where the arrow points to the symbol = - JamesJGoodwin
  • str (success) try ... - Vladimir Martyanov
  • Add flush=True . Otherwise, you may not see the result, for example: print('Requests per minute: {:3d}'.format(rpm), end='\r', flush=True) (if always rpm < 1000 ). - jfs