Print new output on same line [duplicate] Print new output on same line [duplicate] python-3.x python-3.x

Print new output on same line [duplicate]


From help(print):

Help on built-in function print in module builtins:print(...)    print(value, ..., sep=' ', end='\n', file=sys.stdout)    Prints the values to a stream, or to sys.stdout by default.    Optional keyword arguments:    file: a file-like object (stream); defaults to the current sys.stdout.    sep:  string inserted between values, default a space.    end:  string appended after the last value, default a newline.

You can use the end keyword:

>>> for i in range(1, 11):...     print(i, end='')... 12345678910>>> 

Note that you'll have to print() the final newline yourself. BTW, you won't get "12345678910" in Python 2 with the trailing comma, you'll get 1 2 3 4 5 6 7 8 9 10 instead.


* for python 2.x *

Use a trailing comma to avoid a newline.

print "Hey Guys!",print "This is how we print on the same line."

The output for the above code snippet would be,

Hey Guys! This is how we print on the same line.

* for python 3.x *

for i in range(10):    print(i, end="<separator>") # <separator> = \n, <space> etc.

The output for the above code snippet would be (when <separator> = " "),

0 1 2 3 4 5 6 7 8 9


Similar to what has been suggested, you can do:

print(i, end=',')

Output: 0,1,2,3,