What’s ending comma in print function for?

In python 2.7, the comma is to show that the string will be printed on the same line

For example:

for i in xrange(10):
     print i,

This will print

1 2 3 4 5 6 7 8 9 

To do this in python 3 you would do this:

 for i in xrange(10):
      print(i,end=" ")

You will probably find this answer helpful

Printing horizontally in python

—- Edit —

The documentation, http://docs.python.org/2/reference/simple_stmts.html#the-print-statement, says

A ‘\n’ character is written at the end, unless the print statement ends with a comma.

Leave a Comment