使用“ print”时语法无效?

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

print 2 ** 100

这就是 SyntaxError: invalid syntax

指向2。

为什么? 我用的是3.1版

324114 次浏览

你需要括号:

print(2**100)

这是因为在 Python 3中,它们用 print 功能代替了 print 声明

语法现在和以前差不多,但是需要括号:

来自“ 蟒蛇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)!

他们在 Python 3中改变了 print。在2中它是一个语句,现在它是一个函数并且需要括号。

这是 Python 3.0的文档

新的3.x 版本比旧的2.x 版本改变了语法: 例如,在 python 2.x 中,你可以写: 打印“ Hi new world” 但是在新的3.x 版本中,你需要使用新的语法并像下面这样写: 印刷品(“ Hi new world”)

查阅文件: Http://docs.python.org/3.3/library/functions.html?highlight=print#print