0

In python 2, using a comma operator at the end of the statement to print the output in horizontal line. Like this:

for i in range(5):
    print i,

And Above code would generate the following output:

0 1 2 3 4

But, in Python 3 comma operator not work to print the output in horizontal line.

for i in range(5):
    print (i),

Generate the following output:

0
1
2
3
4

So, How to print output in horizontal line in python 3?

Jayesh
  • 4,599
  • 7
  • 28
  • 59
  • 1
    Did you look at the documentation for the print function? – John Coleman Oct 12 '17 at 10:51
  • 3
    There is no such thing as a comma *operator*. A comma is part of a specific expressions, like literals (lists, dicts, sets, tuples) or statements. In Python 3, `print()` is a *function*, and putting a comma after a function doesn't affect what the function does. In Python 2, `print` was a *statement*, where the comma plays a specific part of the statement syntax. – Martijn Pieters Oct 12 '17 at 10:51

1 Answers1

2

In Python 3 you can pass the end argument to the print function to change the end of line character

for i in range(5):
    print(i, end=" ")

>> 0 1 2 3 4
AK47
  • 8,646
  • 6
  • 37
  • 61