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

当在Python 3中尝试使用Python 2的`print`语句时,会出现“SyntaxError: Missing parentheses in call to 'print'”错误。这是因为在Python 3中,`print`变成了一个需要括号的函数。从Python 3.4.2开始,此错误消息是为了帮助从Python 2教程转向Python 3的用户。要解决此问题,只需在要打印的值周围添加括号即可。
摘要由CSDN通过智能技术生成

本文翻译自:What does “SyntaxError: Missing parentheses in call to 'print'” mean in Python?

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? 这意味着什么?


#1楼

参考:https://stackoom.com/question/1ilWJ/在Python中-SyntaxError-调用-print-时缺少括号-是什么意思


#2楼

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: 还为右移运算符引发了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. 由于此错误是在代码运行时(而不是在编译时)引发的,因此它无法访问原始源代码,因此在建议的替换表达式中使用元变量( <message><output_stream> )用户实际键入的内容。 Unlike the syntax error case, it's straightforward to place quotes around the Python expression in the custom right shift error message. 与语法错误的情况不同,在自定义右移错误消息中将引号放在Python表达式周围很简单。


#3楼

In Python 3, you can only print as: 在Python 3中,您只能将其打印为:

print("STRING")

But in Python 2, the parentheses are not necessary. 但是在Python 2中,不需要括号。


#4楼

There is a change in syntax from Python 2 to Python 3. In Python 2, 语法从Python 2更改为Python3。在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差不多。


#5楼

Unfortunately, the old xkcd comic isn't completely up to date anymore. 不幸的是,旧的xkcd漫画不再完全是最新的。

https://i-blog.csdnimg.cn/blog_migrate/bcc0b85f5cedb72a4c15ae8efbe17f63.png

Since Python 3.0 you have to write: 从Python 3.0开始,您必须编写:

print("Hello, World!")

And someone has still to write that antigravity library :( 而且仍然有人要编写antigravity库:(


#6楼

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=', ')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值