Python 3 bytes formatting

社会主义新天地 提交于 2019-11-27 23:24:46

And as of 3.5 % formatting will work for bytes, too!

https://mail.python.org/pipermail/python-dev/2014-March/133621.html

Another way would be:

"{0}, {1}, {2}".format(1, 2, 3).encode()

Tested on IPython 1.1.0 & Python 3.2.3

Interestingly .format() doesn't appear to be supported for byte-sequences; as you have demonstrated.

You could use .join() as suggested here: http://bugs.python.org/issue3982

b", ".join([b'1', b'2', b'3'])

There is a speed advantage associated with .join() over using .format() shown by the BDFL himself: http://bugs.python.org/msg180449

I found the %a working best in Python 3.6.2, it should work both for b"" and "":

print(b"Some stuff %b. Some other stuff" % my_byte_or_unicode_string)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!