F-strings provide a way to embed expressions inside string literals,
using a minimal syntax. It should be noted that an f-string is reallyan expression evaluated at run time, not a constant value. In Python
source code, an f-string is a literal string, prefixed with ‘f’, which
contains expressions inside braces. The expressions are replaced with
their values.
来自PEP498,一个致力于文字字符串插值的PEP(Python增强建议)
这里的要点是突出显示的文本,这意味着由于CPython编译器内部实现了AST(抽象语法树),这使得它们的计算速度更快
此外,我认为它更具可读性,并且在字符串中包含更多变量的情况下,它提供了更好的可读性。使用此语法可以轻松执行多行字符串插值
例如:
相关问题 更多 >
编程相关推荐