受报价影响的F弦

发布于 2025-02-06 17:41:39 字数 145 浏览 2 评论 0原文

alien_o = {"Colour" : "Green"}
print(f"The colour is now {alien_o["Colour"]}.")

这些代码行怎么了?闭合支架和卷发支架受引号的影响,我不明白为什么。

alien_o = {"Colour" : "Green"}
print(f"The colour is now {alien_o["Colour"]}.")

What's wrong with these lines of code? The closing bracket and the curly bracket is affected by the quotes, and I don't understand why.

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

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

发布评论

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

评论(2

Saygoodbye 2025-02-13 17:41:39

混合,尝试使用'color'而不是“ color”。。

它 “现在的颜色为{alien_o [,这不是您想要

。 /whatsnew/3.12.html#whatsnew312-pep701“ rel =“ nofollow noreferrer”> python 3.12 ,OP发布的现在是有效的语法。具体来说,这是新的:这是新的:

报价重复使用:在python 3.11中,重复使用与封闭f-string相同的引号会提高语法,从而迫使用户使用其他可用的引号(例如使用双引号或F-string使用单引号, 。在Python 3.12中,您现在可以做这样的事情

It mixes up the ", try using 'Colour' instead of "Colour".

Because it thinks that the format string is f"The colour is now {alien_o[", which is not what you want.

Python 3.12 and later

As of Python 3.12, what OP posted is now valid syntax. Specifically, this is new:

Quote reuse: in Python 3.11, reusing the same quotes as the enclosing f-string raises a SyntaxError, forcing the user to either use other available quotes (like using double quotes or triple quotes if the f-string uses single quotes). In Python 3.12, you can now do things like this

岁月静好 2025-02-13 17:41:39

直率的答案:

f-strings使用引号作为定界数,这就是为什么在interpolated元素中包含引号{}将解释器混淆的原因。 Python认为您可能正在尝试用内部行情结束F弦。

闭合支架和卷发支架受引号的影响

,如果您关心错误的语义,实际上它不是受影响的关闭括号,而是您的{variable}将其切成一半。

结果,这是一个早期的部分:

The colour is now {alien_o[

由print()解释为字符串,因为它在技术上被引号包裹,而不是dict键插值(插入)插入F弦,您可能意图将其包裹在{}中。

但是,python不知道该消息的 rets ,并在它可以做其他任何事情之前就引出错误。

这就是为什么您可能会看到:

SyntaxError: f-string: unmatched '['

一个可能的解决方案:

如果您问我,在您的情况下,克服这种困惑的最直接方法是消除对引号的需求。

alien_o = {"Colour" : "Green"}
key = "Colour"
print(f"The colour is now {alien_o[key]}.")

# or, if you want to make it more generalized:
print(f"The {key} is now {alien_o[key]}.")

如果那是仍然不满意的话,它不必感觉那么简单:

alien_o = {"Colour" : "Green", 
           "Face" : "Toothy", 
           "Shape" : "None", 
           "Transparency" : "Opaque"}

# dict.keys() gets all dict keys for you
# alien_o is your dict, so for you it is alien_o.keys()

keys = [i for i in alien_o.keys() if len(i)<7]
print(f"The alien's features are now {[alien_o[key] for key in keys]}.")

上面我使用列表理解来添加潜在标准,然后将其传递给一个键 F弦。

您也可以研究:

  • 替代python string插值方法
  • ASCI代码其他有争议的字符(newlines,指定F-strings中简单功能的字符串(我个人

喜欢在F弦上的一个时期:

filename = 'file.txt'
print(f'The name of the file without extension is {filename.split(chr(46)})[0]
# print(f'The name of the file without extension is {filename.split('.'})[0] 

注释的底线将出于与您上面的代码相同的原因而失败。

Straight-forward answer:

f-strings use quotation marks as delimiters, which is why including quotes in the interpolated element {} confuses the interpreter. Python thinks that you may be trying to end the f-string with the interior quotes.

The closing bracket and the curly bracket is affected by the quotes

If you care about the semantics of the error, it's actually not the closing bracket that's affected, but rather your {variable} is chopped in half.

As a result, this earlier part:

The colour is now {alien_o[

is interpreted by print() as a string since it is technically wrapped by quotes, as opposed to a dict key interpolated (inserted) into a f-string, which was likely your intent in wrapping it with {}.

However, python doesn't know what to do with the rest of the message, and throws an error before it can do anything else.

That's why you are likely seeing:

SyntaxError: f-string: unmatched '['

One possible solution:

If you ask me, in your case the most straightforward way to overcome this confusion is to eliminate the need for quotation marks.

alien_o = {"Colour" : "Green"}
key = "Colour"
print(f"The colour is now {alien_o[key]}.")

# or, if you want to make it more generalized:
print(f"The {key} is now {alien_o[key]}.")

If that's still not satisfying, it doesn't have to feel so plain jane:

alien_o = {"Colour" : "Green", 
           "Face" : "Toothy", 
           "Shape" : "None", 
           "Transparency" : "Opaque"}

# dict.keys() gets all dict keys for you
# alien_o is your dict, so for you it is alien_o.keys()

keys = [i for i in alien_o.keys() if len(i)<7]
print(f"The alien's features are now {[alien_o[key] for key in keys]}.")

Above I used a list comprehension to add potential criteria to the keys variable before passing it to an f-string.

You can also look into:

  • alternate Python string interpolation methods
  • asci codes for other controversial characters (newlines, specifying strings for simple functions in f-strings (my personal favorite)

One example of calling the ASCI character code of a period in an f-string:

filename = 'file.txt'
print(f'The name of the file without extension is {filename.split(chr(46)})[0]
# print(f'The name of the file without extension is {filename.split('.'})[0] 

The commented-out bottom line would would fail for the same reason as your code above.

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