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
?
If you want to print your text on the same line in Python 2, you should use a comma at the end of your print statement. Here's an example of this in action: print "Hello there!", print "It is a great day."
Without using loops: * symbol is use to print the list elements in a single line with space. To print all elements in new lines or separated by comma use sep=”\n” or sep=”, ” respectively.
How to print() and input() on the same line in Python # Pass a string to the input() function to have a print statement and input on the same line, e.g. username = input('Enter your username: ') . The input() function takes a prompt string and prints it to standard output without a trailing newline.
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.
Use
from __future__ import print_function
in Python 2 and use print as a function in Python 2 also
print("\r Running... xxx", end="")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With