这个问题已经有一个答案在这里:

为什么我收到的语法错误的时候打印的字符串中Python3?

>>> print "hello World"
  File "<stdin>", line 1
    print "hello World"
                      ^
SyntaxError: invalid syntax
有帮助吗?

解决方案

在Python3, print 成为一个功能.这意味着你需要包括括号现在想提到如下:

print("Hello World")

其他提示

它看起来像你使用Python3.0,在其中 印已经变成一个可调用的功能 而不是一个发言。

print('Hello world!')

因为在Python3, print statement 已经替换了 print() function, 与关键参数来取代大多数特别法的老打印的发言。所以你要把它写作

print("Hello World")

但是如果你写了这个方案和一些使用蟒蛇2.x尝试运行,他们将得到一个错误。为了避免这一点,这是一个很好的做法进口的打印功能

from __future__ import print_function

现在你代码,适用于2.x3.x

以下列出的实例也熟悉打印()function.

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)!

资料来源: 有什么新Python3.0?

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top