我犯了这个错误:
key, value = 'K', 999
msg = (
f"key={key}, "
"value={value}" # needs to be prefixed with f as well
)
# key=K, value={value}
并开始想知道 Python 如何处理复杂的文字连接情况。
假设一个字符串是 f-string(格式化字符串文字),另一个字符串是普通字符串文字,如上例所示。 Python 会在编译时连接这两个字符串吗?如果是,结果是什么?
最佳答案
来自PEP 498 :
Adjacent f-strings and regular strings are concatenated. Regular strings are concatenated at compile time, and f-strings are concatenated at run time.
Each f-string is entirely evaluated before being concatenated to adjacent f-strings.
关于python - 如何在隐式字符串连接中处理 f 字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49982874/