f字符串中允许哪些文字字符



在格式化字符串文字(f-string(的BNF规则中:

f_string ::= (literal_char | "{{" | "}}" | replacement_field)*
...
literal_char ::= <any code point except "{", "}" or NULL>

它对NULL的具体含义是什么?

看起来f''是有效的。

允许的代码点any code point except "{", "}" or NULL

字符串f""包含两个代码点0。它们形成了一个转义序列,表示一个空字节,就像n表示换行一样。这类似于源代码(字符串(True表示布尔真值(对象(的方式
值得注意的是,这些表示并不是它们所代表的东西。将放在f字符串中很好,因为它本身不是NULL。

>>> # the source code representing a number is not the number
>>> source = "1337"
>>> eval(source) == source
False

请注意,CPython和PyPy一开始就不接受源代码中的NULL字节。

最新更新