Print a new issue on the same line

I want to print closed output on the screen in the same line.

How to do this in the easiest way for Python 3.x

I know this question was asked for Python 2.7, using a comma at the end of the line, i.e. print I, but I can not 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,' 

Exit to the screen.

 1 2 3 4 5 6 7 8 9 10 



What I want to print:

 12345678910 

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

+66
python printing
Aug 20 '12 at 4:15
source share
7 answers

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 will have to print() do the last new line yourself. BTW, you will not get "12345678910" in Python 2 with a trailing comma, instead you will get 1 2 3 4 5 6 7 8 9 10 .

+124
Aug 20 2018-12-12T00:
source share

* NOTE: this code is valid only for python 2.x *

Use a trailing comma to avoid a new line.

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

The result for the above code snippet will be,

 Hey Guys! This is how we print on the same line. 
+21
Oct 11 '15 at 17:06
source share

Similar to what was suggested, you can do:

 print(i,end=',') 

Output: 0, 1, 2, 3,

+7
Aug 30 '16 at 20:12
source share

You can do something like:

 >>> print(''.join(map(str,range(1,11)))) 12345678910 
+3
Aug 20 2018-12-12T00:
source share
 print("single",end=" ") print("line") 

it will give a result

 single line 

for the question asked

 i = 0 while i <10: i += 1 print (i,end="") 
+3
Oct 07 '16 at 15:50
source share

Let's take an example where you want to print numbers from 0 to n on one line. You can do this with the following code.

 n=int(raw_input()) i=0 while(i<n): print i, i = i+1 

At the input n = 5

 Output : 0 1 2 3 4 
+2
Aug 25 '16 at 19:15
source share
 >>> for i in range(1, 11): ... print(i, end=' ') ... if i==len(range(1, 11)): print() ... 1 2 3 4 5 6 7 8 9 10 >>> 

Here's how to do it so that printing does not start following the prompt on the next line.

+1
Aug 21 '12 at 18:00
source share



All Articles