gpt4 book ai didi

python - 如何在隐式字符串连接中处理 f 字符串?

转载 作者:行者123 更新时间:2023-12-01 23:27:03 24 4
gpt4 key购买 nike

我犯了这个错误:

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com