用字节格式化字符串

Format string with bytes

有些事情我不明白:此代码适用于 python3.5,但在 python3.4 上给我一个错误:

s='abcd'
s2=b'%s' % s.encode('ascii')

这是错误:

TypeError: unsupported operand type(s) for %: 'bytes' and 'bytes'

知道为什么它适用于 python3.5 而不是 python3.4 吗?

在 Python 3.5 的 bytes 和 bytearray 中添加了对 % 格式的支持 PEP 461:

This PEP proposes adding % formatting operations similar to Python 2's str type to bytes and bytearray.