f 字符串不支持行连接吗?
Is line-joining unsupported by f-strings?
Python 3.6 中的 f-strings 是否不支持以下语法?如果我行加入我的 f 字符串,则不会发生替换:
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)
returns:
This longer message is intended to contain the sub-message here: {SUB_MSG}
如果我删除行连接:
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)
它按预期工作:
This longer message is intended to contain the sub-message here: This is the original message.
在 PEP 498 中,反斜杠 在 中明确不支持 f 字符串:
Escape sequences
Backslashes may not appear inside the expression portions of
f-strings, so you cannot use them, for example, to escape quotes
inside f-strings:
>>> f'{\'quoted string\'}'
行连接是否被视为 'inside the expression portion of f-strings' 因此不受支持?
您必须将两个字符串都标记为 f
-strings 才能使其正常工作,否则第二个字符串将被解释为普通字符串:
SUB_MSG = "This is the original message."
MAIN_MSG = f"test " \
f"{SUB_MSG}"
print(MAIN_MSG)
好吧,在这种情况下,您也可以将第二个字符串设为 f 字符串,因为第一个字符串不包含任何要插入的内容:
MAIN_MSG = "test " \
f"{SUB_MSG}"
请注意,这会影响所有字符串前缀,而不仅仅是 f 字符串:
a = r"\n" \
"\n"
a # '\n\n' <- only the first one was interpreted as raw string
a = b"\n" \
"\n"
# SyntaxError: cannot mix bytes and nonbytes literals
试试这个(注意续行中额外的“f”):
SUB_MSG = "This is the original message."
# f strings must be aligned to comply with PEP and pass linting
MAIN_MSG = f"This longer message is intended to contain " \
f"the sub-message here: {SUB_MSG}"
print(MAIN_MSG)
Python 3.6 中的 f-strings 是否不支持以下语法?如果我行加入我的 f 字符串,则不会发生替换:
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)
returns:
This longer message is intended to contain the sub-message here: {SUB_MSG}
如果我删除行连接:
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)
它按预期工作:
This longer message is intended to contain the sub-message here: This is the original message.
在 PEP 498 中,反斜杠 在 中明确不支持 f 字符串:
Escape sequences
Backslashes may not appear inside the expression portions of f-strings, so you cannot use them, for example, to escape quotes inside f-strings:
>>> f'{\'quoted string\'}'
行连接是否被视为 'inside the expression portion of f-strings' 因此不受支持?
您必须将两个字符串都标记为 f
-strings 才能使其正常工作,否则第二个字符串将被解释为普通字符串:
SUB_MSG = "This is the original message."
MAIN_MSG = f"test " \
f"{SUB_MSG}"
print(MAIN_MSG)
好吧,在这种情况下,您也可以将第二个字符串设为 f 字符串,因为第一个字符串不包含任何要插入的内容:
MAIN_MSG = "test " \
f"{SUB_MSG}"
请注意,这会影响所有字符串前缀,而不仅仅是 f 字符串:
a = r"\n" \
"\n"
a # '\n\n' <- only the first one was interpreted as raw string
a = b"\n" \
"\n"
# SyntaxError: cannot mix bytes and nonbytes literals
试试这个(注意续行中额外的“f”):
SUB_MSG = "This is the original message."
# f strings must be aligned to comply with PEP and pass linting
MAIN_MSG = f"This longer message is intended to contain " \
f"the sub-message here: {SUB_MSG}"
print(MAIN_MSG)