Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Output being printed in the same line, Py3k

In Python 2.x, I'd write...

for i in range(5):
    print i,

...to get integers from 0 to 4 printed in the same row. How to do that in Python 3.x, since print is a function now?

like image 312
Bane Bojanić Avatar asked Jun 05 '10 22:06

Bane Bojanić


People also ask

How do I display output on the same line?

Modify print() method to print on the same line The print method takes an extra parameter end=” “ to keep the pointer on the same line. The end parameter can take certain values such as a space or some sign in the double quotes to separate the elements printed in the same line.

Which statement is used for print output in same line?

The Python's print() function is used to print the result or output to the screen. By default, it jumps to the newline to printing the next statement. It has a pre-defined format to print the output.

How do I print outputs on the same line in Python?

To print on the same line in Python, add a second argument, end=' ', to the print() function call.

How do I print on the same line with two print statements?

To print multiple expressions to the same line, you can end the print statement in Python 2 with a comma ( , ). You can set the end argument to a whitespace character string to print to the same line in Python 3. With Python 3, you do have the added flexibility of changing the end argument to print on the same line.


1 Answers

Use print(x, end = ' '):

From the release notes:

Old: print x,           # Trailing comma suppresses newline  
New: print(x, end=" ")  # Appends a space instead of a newline
like image 136
Mark Byers Avatar answered Sep 28 '22 02:09

Mark Byers