What is usage of the last comma in this code?

前端 未结 2 1830
情歌与酒
情歌与酒 2021-01-17 11:54
for x in range(1, 11):
     print repr(x).rjust(2), repr(x*x).rjust(3),
     # Note trailing comma on previous line
     print repr(x*x*x).rjust(4)

2条回答
  •  走了就别回头了
    2021-01-17 12:14

    It stops print from printing a newline at the end of the text.

    As Dave pointed out, the documentation in python 2.x says: …. "A '\n' character is written at the end, unless the print statement ends with a comma."

    UPDATE:

    The documentation of python 3.x states that print() is a function that accepts the keyword argument end which defaults to a newline, \n.

提交回复
热议问题