What's the rationale for b"..." in this example?

Skip Montanaro skip.montanaro at gmail.com
Tue May 15 11:14:12 EDT 2018


Consider this:

>>> bytes("abc", encoding="utf-8")
b'abc'

Looks reasonable. Then consider this:

>>> str(bytes("abc", encoding="utf-8"))
"b'abc'"

Why is the b'...' bit still there? I suppose it's because I didn't tell it
explicitly how to decode the bytes object, as when I do, I get the expected
result:

>>> str(bytes("abc", encoding="utf-8"), encoding="utf-8")
'abc'

Coming from a still largely Python 2 perspective, did all attempts to apply
default encodings disappear in Python 3?

Skip



More information about the Python-list mailing list