Print in one line on the first in python 2 and 3

There are several solutions for printing on the same line and in the first of them, as shown below.

In python 2,

print "\r Running... xxx",

And in Python 3,

print("\r Running... xxx", end="")

I cannot find one print solution to cover python 2 and 3 at the same time. Should I use sys.stdout.write?

+4
source share
2 answers

Use from __future__ import print_functionand use the function in print()both Python 2 and Python 3:

from __future__ import print_function

print("\r Running... xxx", end="")  # works across both major versions

See the documentation print()in Python 2 :

. , . print(), :

from __future__ import print_function

print() ​​ Python 2.6.

+14

from __future__ import print_function

Python 2 print Python 2

print("\r Running... xxx", end="")
+6

Source: https://habr.com/ru/post/1548318/


All Articles