Print new output on same line

419,062

Solution 1

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.

Solution 2

* 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

Solution 3

Similar to what has been suggested, you can do:

print(i, end=',')

Output: 0,1,2,3,

Solution 4

print("single",end=" ")
print("line")

this will give output

single line

for the question asked use

i = 0 
while i <10:
     i += 1 
     print (i,end="")

Solution 5

You can do something such as:

>>> print(''.join(map(str,range(1,11))))
12345678910
Share:
419,062

Related videos on Youtube

onxx
Author by

onxx

As a net eng I found a constant need to be writing scripts help improve the ease of life.

Updated on January 17, 2020

Comments

  • onxx
    onxx over 4 years

    I want to print the looped output to the screen on the same line.

    How do I this in the simplest way for Python 3.x

    I know this question has been asked for Python 2.7 by using a comma at the end of the line i.e. print I, but I can't find a solution for Python 3.x.

    i = 0 
    while i <10:
         i += 1 
         ## print (i) # python 2.7 would be print i,
         print (i) # python 2.7 would be 'print i,'
    

    Screen output.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    

    What I want to print is:

    12345678910
    

    New readers visit this link aswell http://docs.python.org/release/3.0.1/whatsnew/3.0.html

  • onxx
    onxx almost 12 years
    Thanks. Yes very true that print i, will have an inserted space. What would the code be for it behaving like python 2 with comma?
  • DSM
    DSM almost 12 years
    Same thing but with end = ' '. Instead of ending every print "line" with nothing, you end it with a space.
  • onxx
    onxx almost 12 years
    Thanks for the quick response and update. That makes so much more sense now. I read the help function a dozen times and wasn't obviously paying attention. :)
  • onxx
    onxx almost 12 years
    Read the question. print i, only works in python 2 not python 3.
  • Avichal Badaya
    Avichal Badaya almost 12 years
    Alright, I missed it. I will edit the answer.
  • onxx
    onxx over 8 years
    That only works in in python 2.x this post is for python 3.x print usage
  • lmo
    lmo almost 8 years
    This answer turned up in the low quality review queue, presumably because you don't provide any explanation of the code. If this code answers the question, consider adding adding some text explaining the code in your answer. This way, you are far more likely to get more upvotes — and help the questioner learn something new.
  • Sanket Patel
    Sanket Patel over 7 years
    that is leaving a space. "...Guys!<space>This is...". any workaround?
  • BTR Naidu
    BTR Naidu over 6 years
    How to suppress the space which is inserted because of the comma in the first print statement?
  • soheshdoshi
    soheshdoshi over 5 years
    what about last 3, ?
  • Chandan Kumar
    Chandan Kumar over 4 years
    Thanks, quick and easy.
  • onxx
    onxx almost 4 years
    @imo Actually his code doesn't answer the question, as the question explicitly asks for Python 3.x. Code provided .. print i, ..only works in Python 2