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

标签 python python-3.x f-string

我犯了这个错误:

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/

相关文章:

python - 如何检查变量是否为具有 python 2 和 3 兼容性的字符串

python - Django-CKeditor : How to show RichTextFields in templates

python - 使用 f 字符串固定小数点后的数字

python - 将单词添加到 scikit-learn 的 CountVectorizer 的停止列表

python - 所以我在制作角色动画时遇到了麻烦,而且我是 python 的新手。我不断收到此错误,但我不明白

python - 如何修改Python中存储在列表中的对象的属性

python - 无法在 macOS Catalina 上安装 psycopg2

python - 属性错误: module 'cupy' has no attribute 'array'

python - 如何在 Python 的 print 函数中读取数组的元素?

python - 如何在matplotlib中显示LaTeX f字符串