From the python doc:
Bytes literals are always prefixed with 'b' or 'B'; they produce an instance of the bytes type instead of the str type. They may only contain ASCII characters; bytes with a numeric value of 128 or greater must be expressed with escapes.
I know that I can create a bytes object with b
prefix expression like: b'cool'
, this will convert a unicode string 'cool'
into bytes. I'm aslo aware that bytes instance could be created by bytes()
function but you need to specify the encoding argument: bytes('cool', 'utf-8')
.
From my understaing, I need to use one of the encoding rules if I want to tranlate a string into a sequence of bytes . I have done some experiments and it seems b
prefix converts string into bytes using utf-8 encoding:
>>> a = bytes('a', 'utf-8')
>>> b'a' == a
True
>>> b = bytes('a', 'utf-16')
>>> b'a' == b
False
My question is when creating a bytes object through b
prefix, what encoding does python use? Is there any doc that specifies this question? Does it use utf-8 or ascii as default?