The difference is that CPython 2 uses C stdio to implement standard streams such as sys.stdin, sys.stdout (used by print()) while Python 3 reimplements IO on top of system API e.g., open, read, write on POSIX.
To avoid thinking about how print() is implemented, the same issue occurs if sys.stdout is used directly:
# __main__.py
import sys
sys.stdout.write("input: ")
f = sys.stdin.readline()
sys.stdout.write("*" + f)
On Python 3 "input: " is not printed before the readline() call:
$ python2 .
input: foo bar
*foo bar
$ python3 .
foo bar
input: *foo bar
In C, stdout is flushed before reading any input in the interactive case (it is undefined behavior if an output operation is followed by an input operation on the same update stream without fflush() in between). This C program prints "input: " before asking for input as expected:
#include <stdio.h>
#define MAXLEN 100
int main(void) {
char buf[MAXLEN] = {0};
fputs("input: ", stdout);
if (!fgets(buf, MAXLEN, stdin))
return 1;
fputs(buf, stdout);
return 0;
}
That is why the workaround: calling sys.stdout.flush() before sys.stdin.readline() suggested by @Dietrich Epp works.
It is a deficiency in Python 3 implementation. stdout shall be flushed by default before reading from stdin if both point to the same place (os.path.samefile(sys.stdout.fileno(), sys.stdin.fileno()) e.g., if both tty). You could report the issue at Python bug tracker.
iomodule, available in Python 2 as well), so it is no suprise that flushing behaviour may have changed.from six.moves import input; print("*" + input("input: "))? `rawinputtoinputchange, puttry: input=rawinput\nexcept NameError: pass. at the top of the file.