Print on same line at the first in python 2 and 3

python, python-3.x

Solution

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

from __future__ import print_function

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

See the `print()` function documentation in Python 2:

Note: This function is not normally available as a built-in since the name print is recognized as the print statement. To disable the statement and use the `print()` function, use this future statement at the top of your module:

from __future__ import print_function

The `print()` function was added to Python 2.6.

Problem

There are several solutions for printing on same line and at the first of it, like below. In python 2, ``` print "\r Running... xxx", ``` And in Python 3, ``` print("\r Running... xxx", end="") ``` I can't find a single print solution to cover python 2 and 3 at the same time. Do I have to use `sys.stdout.write`?

Original source