我可以这样创建b-sting:
name_binary = b'Adam'
但是如果我有像 name='Adam' 这样的变量,并且我想立即使用 f-string 和 b-string:< br/> name_binary = fb'{name}'
我得到:
File "<input>", line 1
c = fb'{a}'
^
SyntaxError: invalid syntax
我知道我能做到:
name_binary = name.encode('utf-8')
但是像我的示例一样,同时使用 b 和 f 是否可能实现技术性?
请您参考如下方法:
没有,你想要的已经被提出但到现在都被拒绝了。
在 PEP-489 中阅读更多相关信息:
No binary f-strings
For the same reason that we don't support
bytes.format(), you may not combine'f'with'b'string literals.
您的选择(就像您已经提到的那样)是:
name_binary = f'{name}'.encode('utf-8')
或
name_binary = name.encode('utf-8')
