python - 如何在 f 字符串中解释命名表达式?

标签 python python-3.x string-interpolation f-string walrus-operator

我正在尝试在 f 字符串中使用命名表达式:

print(f"{(a:=5 + 6) = }")

返回:

(a:=5 + 6) = 11

但我希望是这样的:

a = 11

这是否可能,通过组合海象运算符和 f 字符串(这样我就不必先在单独的步骤中声明变量 a)?

最佳答案

print(f"a = {(a:= 5 + 6)}")

来自 this comment 的回答

关于python - 如何在 f 字符串中解释命名表达式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62444232/

相关文章:

python - 使用 df.mask 遮盖某些 Material

python - 来自守护进程 : OCI runtime create failed: container_linux. 的错误响应 go:380:启动容器进程导致:exec: "python":

python-3.x - py.test 中的并行测试

scala - 在字符串插值中转义美元符号

android - Kotlin:如何获取字符串中 "@"之后的字符?

angular - 如何禁用内插字符串 Angular 2+

python - 带有 PyDev 的 PIL 模块

Python 3 if-else 简写命令语法错误

python - 将变量(非模型字段)传递给 Django Rest 框架中的序列化器

Python套接字不传送数据包