Skip to content

Instantly share code, notes, and snippets.

@santosh
Created March 25, 2013 09:18
Show Gist options
  • Select an option

  • Save santosh/5235922 to your computer and use it in GitHub Desktop.

Select an option

Save santosh/5235922 to your computer and use it in GitHub Desktop.
This script demonstrate the `flush` argument of print() function.
#!/usr/bin/env python
#-*- coding: utf-8 -*-
from __future__ import print_function
from time import sleep
string = "The words in this sentence should appear letter by letter."
print("Please wait if you don't see another sentence appearing below.", end="\n\n")
for characters in string:
# If flush disabled the another line will appear at once and not char by char.
print(characters, end="", flush=True)
sleep(.1)
@DeepakMishraDA
Copy link

Sir, flush is not doing anything at all, the sleep func is at work

@DeepakMishraDA
Copy link

please explain

@santosh
Copy link
Author

santosh commented Jul 25, 2020

Can you please elaborate @DeepakMishraDA?

Is it not appearing like this:

Peek 2020-07-26 00-57

@DeepakMishraDA
Copy link

DeepakMishraDA commented Jul 25, 2020 via email

@santosh
Copy link
Author

santosh commented Jul 25, 2020

If you remove flush, it should print the sentence all at once.

@DeepakMishraDA
Copy link

DeepakMishraDA commented Jul 25, 2020 via email

@santosh
Copy link
Author

santosh commented Jul 25, 2020

Something must be wrong with your setup. That is the normal behavior I told before.

@DeepakMishraDA
Copy link

DeepakMishraDA commented Jul 25, 2020 via email

@Gwanza
Copy link

Gwanza commented Oct 9, 2020

I came here through your question about this matter on Stack Overflow and it seems like DeepakMishraDA is right. Flush does not have to do anything in this code. The letters appear one by one because of for loop and sleep in the end. Switching flush to False does the exactly same thing.
I think generally it will be hard to see what flush does unless you have a huge string to print. Human eye can't really differentiate the times it takes for printing between such short string and part of it.

@bug-author
Copy link

No difference with or without flush.
Python 3.9
Windows 10 Pro

@Avinash-Sharma13
Copy link

@shy-tan @Gwanza I tested it on powershell and it works fine, but I was using Jupyter before and it wasn't working on Jupyter.

@smashcoder2003
Copy link

I tried the same on Pycharm it doesn't work, maybe Pycharm doesn't support flush. It only works when you use it on the terminal. Try running the same file through the terminal

@jincminst
Copy link

No difference with or without flush. Python 3.9 Windows 10 Pro

I agree with this

@allurisravanth
Copy link

No, it works as intended. let me explain, all the values passed into print function are buffered until a new line is printed and then the text is flushed to the stream (opened file) or stdout (console).

if there is no new line added after every print function call which can be acheived by adding the optional argument end="" then the text is buffered. by default the value of end is \n

so only way to push the characters to stdout is to flush forcibly which is acheived via flush=True argument

and if flush=False, then you are buffering all the characters and only after the final sleep time the entire text is printed on console

Screen.Recording.2025-12-04.at.10.59.48.AM.mov

@jincminst
Copy link

I post a comment two years ago and now I get an email from github about this gist. Also that this gist was here in 2013.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment