“语法”是什么:在呼叫到“ print”中缺少括号。在python中的意思?

发布于 2025-02-13 23:50:54 字数 551 浏览 2 评论 0原文

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

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

这是什么意思?


请参阅获得使用关键字参数end end end yend =''的syntaxerror。

参见 python 3打印括号用于解决方法,并确认打印不能像的一样工作,就像python 3中的语句。

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

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

What does that mean?


See Getting SyntaxError for print with keyword argument end=' ' for the opposite problem.

See Python 3 print without parenthesis for workarounds, and confirmation that print cannot be made to work like a statement in Python 3.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

╰ゝ天使的微笑 2025-02-20 23:50:54

错误消息 SyntaxError:当您尝试使用Python 3语法与Python 2 Print语句时,会在“ print” 呼叫中丢失括号。

示例:
>

print "Hello, World!"

在Python 3中,打印语句被print()函数替换,需要围绕要打印的值括号。

解决方案

print("Hello, World!")

在Python 3中,打印语句被打印()函数代替,需要围绕要打印的值括号。

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

在Python 3的早期版本中,解释器只是报告了通用语法错误,而没有提供任何可能出现的有用提示:

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

而对于,为什么为什么在Python 3中的功能与语句的基本形式无关,而是与您如何做更复杂的事情,例如用后续空间打印多个项目,而不是结束行。

在Python 2中:

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

在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

从2017年9月的Python 3.6.3发行开始,已经更新了与Python 2.x 2.x打印语法相关的一些错误消息,以推荐其Python 3.x 3.x对应物:

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

自从“自“自从”呼叫打印“ case是一个编译时间语法错误,因此可以访问原始源代码,它可以在建议的其余部分中包含全文 替代品。但是,目前尚未尝试制定适当的报价来围绕该表达式放置(这不是不可能的,只是足够复杂,以至于尚未完成)。

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>)"?

由于代码运行时会增加此错误,而不是在编译时,它无法访问原始源代码,并且因此,在建议的替换表达式中使用meta-variobles(&lt; message&gt; and &lt; output_stream&gt; )打字。与语法错误案例不同,在自定义右移动错误消息中围绕Python表达式的引号很简单。

The error message SyntaxError: Missing parentheses in call to 'print' occurs when you attempt to use Python 3 syntax with the Python 2 print statement.

Example:

print "Hello, World!"

In Python 3, the print statement was replaced with a print() function, requiring parentheses around the value to be printed.

Solution

print("Hello, World!")

In Python 3, the print statement was replaced with a print() function, requiring parentheses around the value to be printed.

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

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

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

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

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

好多鱼好多余 2025-02-20 23:50:54

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

print("Hello, World!")

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

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

Since Python 3.0 you have to write:

print("Hello, World!")

And someone has still to write that antigravity library :(

乖乖 2025-02-20 23:50:54

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

print "Hello, World!" 

将起作用,但在Python 3中使用括号,因为

print("Hello, World!")

这是与Scala相同的语法,靠近Java。

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

print "Hello, World!" 

will work but in Python 3, use parentheses as

print("Hello, World!")

This is equivalent syntax to Scala and near to Java.

扮仙女 2025-02-20 23:50:54

在python 3.x中,print现在是函数,而不是语句,如2.x。

因此,打印调用作为函数使用,因此需要括号:

python 2.x 2.x :打印” “

python 3.x :print(“戒指之王”)

有多个优点

  • 制作print一个函数 打印多个值时更灵活(现在是对函数的参数)。特别是,它允许参数splatting
>>> items = ['foo', 'bar', 'baz']
>>> print(*items, sep='+')
foo+bar+baz
  • 可以通过简单地编写呼叫print> print> print> princt 的行为一个名为打印的新功能。对于打印语句,这是不可能的。

In Python 3.x, print is now a function, rather than a statement as it was in 2.x.

Therefore, print is used by calling it as a function, and thus parentheses are needed:

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

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

There are multiple advantages to making print a function:

  • The print function offers more flexibility when printing multiple values (which are now arguments to the function). In particular, it allows for argument splatting:
>>> items = ['foo', 'bar', 'baz']
>>> print(*items, sep='+')
foo+bar+baz
  • The behavior of calls to print can be replaced by simply writing a new function named print. This would be impossible with the print statement.
≈。彩虹 2025-02-20 23:50:54

如果您的代码在Python 2和3中都可以使用,则可以通过在程序开始时加载它来实现此目标:

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

然后,您可以以Python 3方式打印:

print("python")

如果您想打印某些东西而不创建新行 - 您可以可以这样做:

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

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

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

Then you can print in the Python 3 way:

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=', ')
陈年往事 2025-02-20 23:50:54

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

print("STRING")

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

In Python 3, you can only print as:

print("STRING")

But in Python 2, the parentheses are not necessary.

不知所踪 2025-02-20 23:50:54

我还可以补充说,我了解有关python2.7python3之间的语法更改的一切,并且我的代码被正确地写成print(“ String”)甚至
print(f“ string”) ...

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

python file_name.py

默认情况下使用python2.7来调用我的python脚本,这给出了错误。因此,我将bash脚本更改为:

python3 file_name.py

当然使用python3来运行修复错误的脚本。

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

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

python file_name.py

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

python3 file_name.py

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

无远思近则忧 2025-02-20 23:50:54

print('Hello,World!')

您正在使用Python 3,在打印时需要支架。

print('Hello, World!')

You're using python 3, where you need brackets when printing.

如梦亦如幻 2025-02-20 23:50:54

因此,我遇到了这个错误

from trp import BoundingBox, Document
File "C:\Users\Kshitij Agarwal\AppData\Roaming\Python\Python39\site-packages\trp\__init__.py", line 31
print ip
      ^ 
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(ip)?

,这是一个Python软件包错误,其中使用了Python2,您可能正在Python3上运行此错误。

一种解决方案可能是将python2 转换为Python3打印 print> print(sothings) 在包装文件夹中的每一行中的每一行,这不是一个好主意

So I was getting this error

from trp import BoundingBox, Document
File "C:\Users\Kshitij Agarwal\AppData\Roaming\Python\Python39\site-packages\trp\__init__.py", line 31
print ip
      ^ 
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(ip)?

This is a Python package error, in which Python2 has been used and you are probably running this on Python3.

One solution could be to convert Python2 print something to Python3 print(something) for every line in each file in the package folder, which is not a good idea????. I mean, you can do it but still there are better ways.

To perform the same task, there is a package named 2to3 in Python which converts Python2 scripts to Python3 scripts. To install it, execute the ???? command in terminal..

pip install 2to3

Then change the directory in terminal to the location where the package files are present, in my case - C:\Users\Kshitij Agarwal\AppData\Roaming\Python\Python39\site-packages\trp

Now execute the command ????

2to3 . -w

and voila, all the Python2 files in that directory will be converted to Python3.

Note:- The above commands hold true for other operating systems as well. Only Python package path will vary as per the system.

拥抱影子 2025-02-20 23:50:54

在此处的直接答案之外,应该注意python 2和3之间的另一个关键区别。 几乎所有主要差异都涉及您何时应该使用任何一个版本。 此博客文章也做得很好目前

​python语言。在继续沿Python 3路线继续前,您应该考虑上述文章。您不仅需要更改某些语法,还需要考虑哪些软件包可供您使用(Python 2的优势)以及可以在您的代码中进行的潜在优化(Python 3的优势) 。

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.

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

忆伤 2025-02-20 23:50:54

打印“文本”不是在Python中打印文本的方式,因为这行不通
print(“ text”)将在命令行中的屏幕上打印上述文本

print "text" is not the way of printing text in python as this won't work
print("text") will print said text on your screen in the command line

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文