尝试从 f 字符串打印字典:
import pprint as pp
my_dict = {
"key_a": ["here", "are", "the", "things"]
, "key_b": ["that", "i", "want"]
, "key_b": ["to", "share", "with", "the", "worlddddddddddddddddddddd"]
}
pp.pprint(f"Let's talk about all of these things:\n\n{my_dict}")
Run Code Online (Sandbox Code Playgroud)
输出(不漂亮):
("Let's talk about all of these things:\n"
'\n'
"{'key_a': ['here', 'are', 'the', 'things'], 'key_b': ['to', 'share', 'with', "
"'the', 'worlddddddddddddddddddddd']}")
Run Code Online (Sandbox Code Playgroud)
有没有办法让这个在 f 字符串中工作,这样我就不必这样做pp.pprint(my_dict)?
对于以下行:
print("{0: <24}".format("==> core=") + str(my_dict["core"]))
Run Code Online (Sandbox Code Playgroud)
我收到以下警告消息:
[考虑使用 f 字符串] 格式化可能是 f 字符串的常规字符串 [C0209]
我可以使用重新格式化它吗f-string?
我有以下 f 字符串:
f"Something{function(parameter)}"
Run Code Online (Sandbox Code Playgroud)
我想对该参数进行硬编码,它是一个字符串:
f"Something{function("foobar")}"
Run Code Online (Sandbox Code Playgroud)
它给了我这个错误:
SyntaxError: f-string: unmatched '('
Run Code Online (Sandbox Code Playgroud)
我该怎么做呢?
为什么f'{'one'}'语法无效?在我看来,如果 f 字符串是明智的,那么一旦 a{出现,其中的所有内容都{ ... }应该被视为 Python 代码。
语言设计者决定将字符串的结束引号视为字符串结束的原因是什么,即使是在 的内部{ ... }?
由此带来什么好处?
关闭后编辑
注意:这是一个老问题。从 python 3.12 开始,这实际上是有效的语法。请参阅:https ://docs.python.org/3/whatsnew/3.12.html#whatsnew312-pep701
Python 3.6中的f字符串不支持以下语法吗?如果我连接我的f-string,则不会发生替换:
SUB_MSG = "This is the original message."
MAIN_MSG = f"This longer message is intended to contain " \
"the sub-message here: {SUB_MSG}"
print(MAIN_MSG)
Run Code Online (Sandbox Code Playgroud)
收益:
This longer message is intended to contain the sub-message here: {SUB_MSG}
Run Code Online (Sandbox Code Playgroud)
如果我删除了行连接:
SUB_MSG = "This is the original message."
MAIN_MSG = f"This longer message is intended to contain the sub-message here: {SUB_MSG}"
print(MAIN_MSG)
Run Code Online (Sandbox Code Playgroud)
它按预期工作:
This longer message is intended to contain the sub-message here: This is the original message.
Run Code Online (Sandbox Code Playgroud)
在PEP 498,反斜杠中 …
我有一些字符串,它们最初用加号连接并包含正则表达式字符串。下面是一个例子:
"Level 4: " + re.sub(r"(\w)([A-Z])", r"\1 \2", talents[1]) + "\n\n"
Run Code Online (Sandbox Code Playgroud)
但是,我一直想使用更合适的格式。我对 f 弦做了一些研究,我想在这里使用它们。我是这样试的:
f'Level 4: {re.sub(r"(\w)([A-Z])", r"\1 \2", talents[1])} \n\n'
Run Code Online (Sandbox Code Playgroud)
然而,我的编辑正在对我咆哮关于带有反斜杠的表达式片段。在这种情况下,f-strings 不是适合这项工作的工具吗?
编辑:根据@jwodder 的要求,这是我从 Python 中得到的错误(我在 3.6 上)
SyntaxError: f-string expression part cannot include a backslash
Run Code Online (Sandbox Code Playgroud) 我正在使用Python 3.x,我正在尝试从__repr__函数中获取一个f-string报告,但我似乎无法获得以下格式化字符串,以便按照我期望的方式工作.
我经常收到"SyntaxError:解析时意外的EOF"
def __repr__(self):
return f"Player has {'Soft' if self.soft > 0} {self.count}. Cards are {self.cards}."
Run Code Online (Sandbox Code Playgroud)
如果self.soft> 0},则给出错误的部分是{'Soft'.如果不清楚,我试图包含"Soft"IFF self.soft> 0这个词,如果没有,不要在字符串中添加任何单词.
我正在为python / pandas做学校作业,想尝试一下f字符串,因为这似乎是非常方便的格式化方式。
阅读文档后,我意识到我无法使用\ n格式化输出。对于此代码:
f"Shape of dataset:\n {df.shape} (rows, columns)\n"
Run Code Online (Sandbox Code Playgroud)
我得到以下输出:
f"Shape of dataset:\n {df.shape} (rows, columns)\n"
Run Code Online (Sandbox Code Playgroud)
阅读文档后,这正是我所期望的。
但是,当我用包围时print(),如下所示:
print(f"Shape of dataset:\n {df.shape} (rows, columns)\n")
Run Code Online (Sandbox Code Playgroud)
我以我想要的方式找到它:
Out[38]: 'Shape of dataset:\n (270792, 11) (rows, columns)\n'
Run Code Online (Sandbox Code Playgroud)
我知道我也可以使用常规格式,但是我很好奇为什么这样做。是因为f字符串组件被忽略了print吗?
我在 VSCode 和 Code Runner 扩展中遇到了这个问题,以下是导致该问题的代码片段:
class init_error(Exception):
def __init__(self, comp="Internals"):
self.component = comp
self.message = f"Error occurred while initiating {self.component}"
def __str__(self):
return self.message
Run Code Online (Sandbox Code Playgroud)
我一开始以为是Python2和Python3之间的编译器搞错了,但在指定#!/usr/bin/env python3并检查是否print("foo")有效后,我相当确定这不是版本问题。我已检查 Code Runner 中的编译器设置为3.7.4 64-bit,因此我尝试通过扩展运行代码Python,并且它有效,所以我相信这是 Code Runner 问题。
很抱歉很长,但最后,f 字符串没有用红色下划线,并且不会出现问题,因此出于某种原因,f 字符串被认为是有效的语法,但不仅仅在代码中运行跑步者延伸。
如何让 Code Runner 接受 f 字符串?
感谢您的帮助。
PS 我认为这无关紧要,但我可以在一个月前发誓它有效。
python python-3.x visual-studio-code f-string vscode-code-runner
我遇到了以下问题f-string:
>>> a='hello'
# how to print '{hello}' ?
>>> f'{{a}}'
'{a}'
>>> f'\{{a}\}'
File "<stdin>", line 1
SyntaxError: f-string: single '}' is not allowed
# doing it without f-strings
>>> '{' + a + '}'
'{hello}'
Run Code Online (Sandbox Code Playgroud)
如何转义 fstring 中的字符?
f-string ×10
python ×9
python-3.x ×4
string ×2
eof ×1
pprint ×1
pylint ×1
python-3.6 ×1
regex ×1
repr ×1