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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25445439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:13:23  来源:igfitidea点击:

What does "SyntaxError: Missing parentheses in call to 'print'" mean in Python?

pythonpython-3.x

提问by ncoghlan

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

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

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

What does that mean?

这意味着什么?

采纳答案by ncoghlan

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 printstatement:

此错误消息表示您正在尝试使用 Python 3 来遵循示例或运行使用 Python 2print语句的程序:

print "Hello, World!"
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: Missing parentheses in call to '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 whyprintbecame 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 TypeErrorraised for the right shift operator has also been customised:

TypeError右移运算符的凸起也已定制:

>>> 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.

由于此错误是在代码运行时而非编译时引发的,因此它无法访问原始源代码,因此在建议的替换表达式中使用元变量 ( <message>and <output_stream>) 而不是用户实际键入的内容. 与语法错误情况不同,在自定义右移错误消息中的 Python 表达式周围放置引号很简单。

回答by Larry

In Python 3, you can only print as:

在 Python 3 中,您只能打印为:

print("STRING")

But in Python 2, the parentheses are not necessary.

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

回答by Sagar balai

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 parentheses as

会工作,但在 Python 3 中,使用括号作为

print("Hello, World!")

This is equivalent syntax to Scala and near to Java.

这是与 Scala 等效的语法,并且接近于 Java。

回答by Christian

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

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

https://imgs.xkcd.com/comics/python.png

https://imgs.xkcd.com/comics/python.png

Since Python 3.0 you have to write:

从 Python 3.0 开始,您必须编写:

print("Hello, World!")

And someone has still to write that antigravitylibrary :(

还有人必须写那个antigravity库:(

回答by Lukasz

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=', ')

回答by Chad Van De Hey

Outside of the direct answers here, one should note the other key difference between python 2 and 3. The official python wikigoes into almost all of the major differences and focuses on when you should use either of the versions. This blog postalso 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 的优势) .

回答by Om Sao

Basically, since Python 3.x you need to use printwith parenthesis.

基本上,从 Python 3.x 开始,您需要使用print括号。

Python 2.x: print "Lord of the Rings"

Python 2.x:打印“指环王”

Python 3.x: print("Lord of the Rings")

Python 3.x:打印(“指环王”)



Explaination

说明

printwas a statementin 2.x, but it's a functionin 3.x. Now, there are a number of good reasons for this.

print是一个声明2.x的,但它是一个功能3.X。现在,有很多很好的理由。

  1. With function format of Python 3.x, more flexibility comes when printing multiple items with comman separated.
  2. You can't use argument splatting with a statement. In 3.x if you have a list of items that you want to print with a separator, you can do this:
  1. 使用 Python 3.x 的函数格式,在打印多个项目时使用逗号分隔具有更大的灵活性。
  2. 你不能在语句中使用参数。在 3.x 中,如果您有要使用分隔符打印的项目列表,您可以这样做:
>>> items = ['foo', 'bar', 'baz']
>>> print(*items, sep='+') 
foo+bar+baz
>>> items = ['foo', 'bar', 'baz']
>>> print(*items, sep='+') 
foo+bar+baz
  1. You can't override a statement. If you want to change the behavior of print, you can do that when it's a function but not when it's a statement.
  1. 您不能覆盖语句。如果你想改变 print 的行为,你可以在它是一个函数时这样做,而不是在它是一个语句时。

回答by Alfa Bravo

I could also just add that I knew everything about the syntax change between Python2.7and Python3, and my code was correctly written as print("string")and even print(f"string")...

我也可以只补充一点,我知道的语法改变一切Python2.7Python3,和我的代码是正确写成print("string"),甚至 print(f"string")...

But after some time of debugging I realized that my bash script was calling python like:

但是经过一段时间的调试后,我意识到我的 bash 脚本正在调用 python,如下所示:

python file_name.py

蟒蛇文件名.py

which had the effect of calling my python script by default using python2.7which gave the error. So I changed my bash script to:

默认情况下python2.7,它具有调用我的 python 脚本的效果,使用which 给出了错误。所以我将我的 bash 脚本更改为:

python3 file_name.py

python3 文件名.py

which of coarse uses python3 to run the script which fixed the error.

哪个粗略使用python3来运行修复错误的脚本。