使用"print"时语法无效?

Lon*_*ice 109 python

我正在学习Python,甚至不能编写第一个例子:

print 2 ** 100
Run Code Online (Sandbox Code Playgroud)

这给了 SyntaxError: invalid syntax

指着2.

为什么是这样?我正在使用3.1版

TM.*_*TM. 225

这是因为在Python 3,他们已经取代了print 声明print 功能.

语法现在或多或少与以前相同,但它需要parens:

来自" python 3中的新功能 "文档:

Old: print "The answer is", 2*2
New: print("The answer is", 2*2)

Old: print x,           # Trailing comma suppresses newline
New: print(x, end=" ")  # Appends a space instead of a newline

Old: print              # Prints a newline
New: print()            # You must call the function!

Old: print >>sys.stderr, "fatal error"
New: print("fatal error", file=sys.stderr)

Old: print (x, y)       # prints repr((x, y))
New: print((x, y))      # Not the same as print(x, y)!
Run Code Online (Sandbox Code Playgroud)

  • 请参阅此内容以获取更多信息:[为什么print语句不是pythonic?](http://stackoverflow.com/questions/1053849/why-print-statement-is-not-pythonic) (3认同)

Joh*_*sch 14

你需要括号:

print(2**100)
Run Code Online (Sandbox Code Playgroud)


Sch*_*ern 8

他们print在Python 3中改变了.在2中它是一个声明,现在它是一个函数并需要括号.

这是Python 3.0的文档.