在Python中,“SyntaxError: 'print'调用中缺少圆括号”是什么意思?

时间:2022-02-01 10:26:26

When I try to use a print statement in Python, it gives me this error:

当我尝试在Python中使用print语句时,它会给我这个错误:

>>> print "Hello, World!"
  File "<stdin>", line 1
    print "Hello, World!"
                        ^
SyntaxError: Missing parentheses in call to 'print'

What does that mean?

这是什么意思?

6 个解决方案

#1


519  

This error message means that you are attempting to use Python 3 to follow an example or run a program that uses the Python 2 print statement:

这个错误消息意味着您正在尝试使用Python 3来遵循一个示例,或者运行一个使用Python 2 print语句的程序:

print "Hello, World!"

The statement above does not work in Python 3. In Python 3 you need to add parentheses around the value to be printed:

上面的语句在Python 3中不起作用。在Python 3中,您需要在要打印的值周围添加圆括号:

print("Hello, World!")

“SyntaxError: Missing parentheses in call to 'print'” is a new error message that was added in Python 3.4.2 primarily to help users that are trying to follow a Python 2 tutorial while running Python 3.

“SyntaxError:在'print'调用中缺少圆括号”是在Python 3.4.2中添加的一个新的错误消息,主要用于帮助那些在运行Python 3时尝试遵循Python 2教程的用户。

In Python 3, printing values changed from being a distinct statement to being an ordinary function call, so it now needs parentheses:

在Python 3中,打印值从一个独立的语句变成了一个普通的函数调用,所以现在需要圆括号:

>>> print("Hello, World!")
Hello, World!

In earlier versions of Python 3, the interpreter just reports a generic syntax error, without providing any useful hints as to what might be going wrong:

在Python 3的早期版本中,解释器只报告了一个通用语法错误,没有提供任何有用的线索来说明哪里出错了:

>>> print "Hello, World!"
  File "<stdin>", line 1
    print "Hello, World!"
                        ^
SyntaxError: invalid syntax

As for why print became an ordinary function in Python 3, that didn't relate to the basic form of the statement, but rather to how you did more complicated things like printing multiple items to stderr with a trailing space rather than ending the line.

至于为什么print在Python 3中成为一个普通的函数,这与语句的基本形式无关,而是与如何将多个条目打印到stderr而不是结束行有关。

In Python 2:

在Python中2:

>>> import sys
>>> print >> sys.stderr, 1, 2, 3,; print >> sys.stderr, 4, 5, 6
1 2 3 4 5 6

In Python 3:

在Python 3:

>>> import sys
>>> print(1, 2, 3, file=sys.stderr, end=" "); print(4, 5, 6, file=sys.stderr)
1 2 3 4 5 6

Starting with the Python 3.6.3 release in September 2017, some error messages related to the Python 2.x print syntax have been updated to recommend their Python 3.x counterparts:

从2017年9月的Python 3.6.3版本开始,一些与Python 2相关的错误消息。已经更新了x打印语法以推荐他们的Python 3。x同行:

>>> print "Hello!"
  File "<stdin>", line 1
    print "Hello!"
                 ^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print("Hello!")?

Since the "Missing parentheses in call to print" case is a compile time syntax error and hence has access to the raw source code, it's able to include the full text on the rest of the line in the suggested replacement. However, it doesn't currently try to work out the appropriate quotes to place around that expression (that's not impossible, just sufficiently complicated that it hasn't been done).

由于“调用中缺少的圆括号来打印”的情况是一个编译时语法错误,因此可以访问原始源代码,因此可以在建议的替换中包含其余行的全文。然而,它目前并没有试图找到合适的引号来围绕这个表达式(这不是不可能的,只是非常复杂,还没有完成)。

The TypeError raised for the right shift operator has also been customised:

右移位操作符的类型错误也被定制:

>>> print >> sys.stderr
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for >>: 'builtin_function_or_method' and '_io.TextIOWrapper'. Did you mean "print(<message>, file=<output_stream>)"?

Since this error is raised when the code runs, rather than when it is compiled, it doesn't have access to the raw source code, and hence uses meta-variables (<message> and <output_stream>) in the suggested replacement expression instead of whatever the user actually typed. Unlike the syntax error case, it's straightforward to place quotes around the Python expression in the custom right shift error message.

由于该错误是在代码运行时引起的,而不是在编译时引起的,因此它不能访问原始源代码,因此在建议的替换表达式中使用元变量( ),而不是用户实际输入的内容。与语法错误的情况不同,在自定义右移位错误消息中围绕Python表达式放置引号非常简单。

#2


8  

There is a change in syntax from Python 2 to Python 3. In Python 2,

从Python 2到Python 3,语法发生了变化。在Python中2,

 print "Hello, World!" will work but

In Python 3, use braces as

在Python 3中,使用大括号as

print("Hello, World!")

This is equivalent syntax to Scala and near to Java.

这相当于Scala的语法,接近Java。

#3


7  

Unfortunately, the old xkcd comic isn't completely up to date anymore.

不幸的是,旧的xkcd漫画并不是完全最新的。

在Python中,“SyntaxError: 'print'调用中缺少圆括号”是什么意思?

Since Python 3.0 you have to write:

因为Python 3.0需要编写:

print("Hello, World!")

And someone has still to write that antigravity library :(

还有人要写反重力图书馆

#4


3  

In Python 3, you can only print as:

在Python 3中,只能打印为:

print("STRING")

But in Python 2, the parentheses are not necessary.

但是在Python 2中,圆括号是不必要的。

#5


2  

If your code should work in both Python 2 and 3, you can achieve this by loading this at the beginning of your program:

如果您的代码应该在Python 2和3中工作,您可以通过在程序开始时加载它来实现这一点:

from __future__ import print_function   # If code has to work in Python 2 and 3!

Then you can print in the Python 3 way:

然后你可以用Python 3方式打印:

print("python")

If you want to print something without creating a new line - you can do this:

如果你想打印一些东西而不创建一个新的行-你可以这样做:

for number in range(0, 10):
    print(number, end=', ')

#6


0  

Outside of the direct answers here, one should note the other key difference between python 2 and 3. The official python wiki goes into almost all of the major differences and focuses on when you should use either of the versions. This blog post also does a fine job of explaining the current python universe and the somehow unsolved puzzle of moving to python 3.

除了这里的直接答案之外,还应该注意到python 2和3之间的其他关键区别。官方的python wiki介绍了几乎所有的主要差异,并重点介绍了什么时候应该使用这两个版本。这篇博客文章也很好地解释了当前的python世界以及迁移到python 3的未解之谜。

As far as I can tell, you are beginning to learn the python language. You should consider the aforementioned articles before you continue down the python 3 route. Not only will you have to change some of your syntax, you will also need to think about which packages will be available to you (an advantage of python 2) and potential optimizations that could be made in your code (an advantage of python 3).

据我所知,您正在开始学习python语言。在继续学习python 3之前,您应该考虑前面提到的文章。您不仅需要更改一些语法,还需要考虑哪些包将对您可用(python 2的一个优点)以及可以在代码中进行的潜在优化(python 3的一个优点)。

#1


519  

This error message means that you are attempting to use Python 3 to follow an example or run a program that uses the Python 2 print statement:

这个错误消息意味着您正在尝试使用Python 3来遵循一个示例,或者运行一个使用Python 2 print语句的程序:

print "Hello, World!"

The statement above does not work in Python 3. In Python 3 you need to add parentheses around the value to be printed:

上面的语句在Python 3中不起作用。在Python 3中,您需要在要打印的值周围添加圆括号:

print("Hello, World!")

“SyntaxError: Missing parentheses in call to 'print'” is a new error message that was added in Python 3.4.2 primarily to help users that are trying to follow a Python 2 tutorial while running Python 3.

“SyntaxError:在'print'调用中缺少圆括号”是在Python 3.4.2中添加的一个新的错误消息,主要用于帮助那些在运行Python 3时尝试遵循Python 2教程的用户。

In Python 3, printing values changed from being a distinct statement to being an ordinary function call, so it now needs parentheses:

在Python 3中,打印值从一个独立的语句变成了一个普通的函数调用,所以现在需要圆括号:

>>> print("Hello, World!")
Hello, World!

In earlier versions of Python 3, the interpreter just reports a generic syntax error, without providing any useful hints as to what might be going wrong:

在Python 3的早期版本中,解释器只报告了一个通用语法错误,没有提供任何有用的线索来说明哪里出错了:

>>> print "Hello, World!"
  File "<stdin>", line 1
    print "Hello, World!"
                        ^
SyntaxError: invalid syntax

As for why print became an ordinary function in Python 3, that didn't relate to the basic form of the statement, but rather to how you did more complicated things like printing multiple items to stderr with a trailing space rather than ending the line.

至于为什么print在Python 3中成为一个普通的函数,这与语句的基本形式无关,而是与如何将多个条目打印到stderr而不是结束行有关。

In Python 2:

在Python中2:

>>> import sys
>>> print >> sys.stderr, 1, 2, 3,; print >> sys.stderr, 4, 5, 6
1 2 3 4 5 6

In Python 3:

在Python 3:

>>> import sys
>>> print(1, 2, 3, file=sys.stderr, end=" "); print(4, 5, 6, file=sys.stderr)
1 2 3 4 5 6

Starting with the Python 3.6.3 release in September 2017, some error messages related to the Python 2.x print syntax have been updated to recommend their Python 3.x counterparts:

从2017年9月的Python 3.6.3版本开始,一些与Python 2相关的错误消息。已经更新了x打印语法以推荐他们的Python 3。x同行:

>>> print "Hello!"
  File "<stdin>", line 1
    print "Hello!"
                 ^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print("Hello!")?

Since the "Missing parentheses in call to print" case is a compile time syntax error and hence has access to the raw source code, it's able to include the full text on the rest of the line in the suggested replacement. However, it doesn't currently try to work out the appropriate quotes to place around that expression (that's not impossible, just sufficiently complicated that it hasn't been done).

由于“调用中缺少的圆括号来打印”的情况是一个编译时语法错误,因此可以访问原始源代码,因此可以在建议的替换中包含其余行的全文。然而,它目前并没有试图找到合适的引号来围绕这个表达式(这不是不可能的,只是非常复杂,还没有完成)。

The TypeError raised for the right shift operator has also been customised:

右移位操作符的类型错误也被定制:

>>> print >> sys.stderr
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for >>: 'builtin_function_or_method' and '_io.TextIOWrapper'. Did you mean "print(<message>, file=<output_stream>)"?

Since this error is raised when the code runs, rather than when it is compiled, it doesn't have access to the raw source code, and hence uses meta-variables (<message> and <output_stream>) in the suggested replacement expression instead of whatever the user actually typed. Unlike the syntax error case, it's straightforward to place quotes around the Python expression in the custom right shift error message.

由于该错误是在代码运行时引起的,而不是在编译时引起的,因此它不能访问原始源代码,因此在建议的替换表达式中使用元变量( ),而不是用户实际输入的内容。与语法错误的情况不同,在自定义右移位错误消息中围绕Python表达式放置引号非常简单。

#2


8  

There is a change in syntax from Python 2 to Python 3. In Python 2,

从Python 2到Python 3,语法发生了变化。在Python中2,

 print "Hello, World!" will work but

In Python 3, use braces as

在Python 3中,使用大括号as

print("Hello, World!")

This is equivalent syntax to Scala and near to Java.

这相当于Scala的语法,接近Java。

#3


7  

Unfortunately, the old xkcd comic isn't completely up to date anymore.

不幸的是,旧的xkcd漫画并不是完全最新的。

在Python中,“SyntaxError: 'print'调用中缺少圆括号”是什么意思?

Since Python 3.0 you have to write:

因为Python 3.0需要编写:

print("Hello, World!")

And someone has still to write that antigravity library :(

还有人要写反重力图书馆

#4


3  

In Python 3, you can only print as:

在Python 3中,只能打印为:

print("STRING")

But in Python 2, the parentheses are not necessary.

但是在Python 2中,圆括号是不必要的。

#5


2  

If your code should work in both Python 2 and 3, you can achieve this by loading this at the beginning of your program:

如果您的代码应该在Python 2和3中工作,您可以通过在程序开始时加载它来实现这一点:

from __future__ import print_function   # If code has to work in Python 2 and 3!

Then you can print in the Python 3 way:

然后你可以用Python 3方式打印:

print("python")

If you want to print something without creating a new line - you can do this:

如果你想打印一些东西而不创建一个新的行-你可以这样做:

for number in range(0, 10):
    print(number, end=', ')

#6


0  

Outside of the direct answers here, one should note the other key difference between python 2 and 3. The official python wiki goes into almost all of the major differences and focuses on when you should use either of the versions. This blog post also does a fine job of explaining the current python universe and the somehow unsolved puzzle of moving to python 3.

除了这里的直接答案之外,还应该注意到python 2和3之间的其他关键区别。官方的python wiki介绍了几乎所有的主要差异,并重点介绍了什么时候应该使用这两个版本。这篇博客文章也很好地解释了当前的python世界以及迁移到python 3的未解之谜。

As far as I can tell, you are beginning to learn the python language. You should consider the aforementioned articles before you continue down the python 3 route. Not only will you have to change some of your syntax, you will also need to think about which packages will be available to you (an advantage of python 2) and potential optimizations that could be made in your code (an advantage of python 3).

据我所知,您正在开始学习python语言。在继续学习python 3之前,您应该考虑前面提到的文章。您不仅需要更改一些语法,还需要考虑哪些包将对您可用(python 2的一个优点)以及可以在代码中进行的潜在优化(python 3的一个优点)。