W3docs

How can I flush the output of the print function?

To flush the output of the print function, you can use the flush parameter of the print function.

To flush the output of the print function, you can use the flush parameter of the print function. By default, the flush parameter is set to False, which means that the output is buffered and may not be displayed until the buffer is full or is flushed. Setting the flush parameter to True will cause the output to be displayed immediately, even if the output buffer is not full.

Here is an example of how you can use the flush parameter:

Flush the output of the print function in Python

print("This output will be flushed immediately", flush=True)

<div class="alert alert-info flex not-prose"> Watch a course <span class="hidden md:block">Watch a video course </span> Python - The Practical Guide</div>

You can also use the flush method of the sys.stdout object to flush the output buffer:

Flush the output buffer using the sys module in Python

import sys

print("This output will be buffered")
sys.stdout.flush()

Keep in mind that flushing the output buffer may not have any effect on some systems, or may result in a slower output.