What is the difference between print and print() in python 2.7

后端 未结 5 839
我在风中等你
我在风中等你 2020-12-06 02:29

I am newbie on Python.

I run the following code on python 2.7 and I see different result when I use print or print(). What is the difference between these two functi

5条回答
  •  天涯浪人
    2020-12-06 02:57

    This in mainly a complement to other answers.

    You can see in Python 2 scripts print (var) when the normal usage would be print var.

    It uses the fact that (var) is just a parenthesed expression in Python 2 with is simply seen as var so print(var) and print var behaves exactly the same in Python 2 - but only works when printing one single variable

    The interesting point is that when you considere a migration to Python 3, print(var) (here the call to function print) is already the correct syntax.

    TL/DR: print(var) in Python 2 is just a trick to ease Python3 migration using the fact that (var) is just an expression - the tuple form would be (var,)

提交回复
热议问题