Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

printing to the screen on the same line at different times

Tags:

python

My code looks like this:

print "Doing Something...",
do_some_function_that_takes_a_long_time()
print "Done"

I want it to print that statement at the top to the screen first, then do the function, and then print the "Done" line. Currently it waits until the "Done" line is executed before it prints the top part with it.

In other words, I want the pausing to occur with "Doing Something..." on the screen. How do I achieve this?

like image 852
priestc Avatar asked Apr 19 '10 21:04

priestc


People also ask

How do I print the same line multiple times?

To print a string multiple times: Use the multiplication operator to repeat the string N times. Use the print() function to print the result. The print() function will print the string repeated the specified number of times.

How do I get my code to print on the same line?

To print on the same line in Python, add a second argument, end=' ', to the print() function call.


1 Answers

You have to flush the output after your first print.

like image 82
vladr Avatar answered Sep 21 '22 18:09

vladr