multiple prints on the same line in Python multiple prints on the same line in Python python python

multiple prints on the same line in Python


You can use the print statement to do this without importing sys.

def install_xxx():   print "Installing XXX...      ",install_xxx()print "[DONE]"

The comma on the end of the print line prevents print from issuing a new line (you should note that there will be an extra space at the end of the output).

The Python 3 Solution
Since the above does not work in Python 3, you can do this instead (again, without importing sys):

def install_xxx():    print("Installing XXX...      ", end="", flush=True)install_xxx()print("[DONE]")

The print function accepts an end parameter which defaults to "\n". Setting it to an empty string prevents it from issuing a new line at the end of the line.


You can simply use this:

print 'something',...print ' else',

and the output will be

something else

no need to overkill by import sys. Pay attention to comma symbol at the end.

Python 3+print("some string", end=""); to remove the newline insert at the end. Read more by help(print);


You should use backspace '\r' or ('\x08') char to go back on previous position in console output

Python 2+:

import timeimport sysdef backspace(n):    sys.stdout.write((b'\x08' * n).decode()) # use \x08 char to go back   for i in range(101):                        # for 0 to 100    s = str(i) + '%'                        # string for output    sys.stdout.write(s)                     # just print    sys.stdout.flush()                      # needed for flush when using \x08    backspace(len(s))                       # back n chars        time.sleep(0.2)                         # sleep for 200ms

Python 3:

import time   def backline():            print('\r', end='')                     # use '\r' to go backfor i in range(101):                        # for 0 to 100    s = str(i) + '%'                        # string for output    print(s, end='')                        # just print and flush    backline()                              # back to the beginning of line        time.sleep(0.2)                         # sleep for 200ms

This code will count from 0% to 100% on one line. Final value will be:

> python test.py100%

Additional info about flush in this case here: Why do python print statements that contain 'end=' arguments behave differently in while-loops?