To print on the same line in Python, add a second argument, end=' ', to the print() function call. print("It's me.")
Python print() FunctionThe print() function prints the specified message to the screen, or other standard output device. The message can be a string, or any other object, the object will be converted into a string before written to the screen.
You can also use the carriage return:
sys.stdout.write("Download progress: %d%% \r" % (progress) )
sys.stdout.flush()
I like the following:
print 'Downloading File FooFile.txt [%d%%]\r'%i,
Demo:
import time
for i in range(100):
time.sleep(0.1)
print 'Downloading File FooFile.txt [%d%%]\r'%i,
print('Downloading File FooFile.txt [%d%%]\r'%i, end="")
Demo:
import time
for i in range(100):
time.sleep(0.1)
print('Downloading File FooFile.txt [%d%%]\r'%i, end="")
# On PyCharm Debugger console, \r needs to come before the text.
# Otherwise, the text may not appear at all, or appear inconsistently.
# tested on PyCharm 2019.3, Python 3.6
import time
print('Start.')
for i in range(100):
time.sleep(0.02)
print('\rDownloading File FooFile.txt [%d%%]'%i, end="")
print('\nDone.')
Use a terminal-handling library like the curses module:
The curses module provides an interface to the curses library, the de-facto standard for portable advanced terminal handling.
Print the backspace character \b
several times, and then overwrite the old number with the new number.
For Python 3xx:
import time
for i in range(10):
time.sleep(0.2)
print ("\r Loading... {}".format(i)+str(i), 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