Could you explain in detail what the difference is between byte string and Unicode string in Python. I have read this:
Byte code is simply the converted source code into arrays of bytes
Does it mean that Python has its own coding/encoding format? Or does it use the operation system settings?
I don’t understand. Could you please explain?
Thank you!
No, Python does not use its own encoding – it will use any encoding that it has access to and that you specify.
A character in a
strrepresents one Unicode character. However, to represent more than 256 characters, individual Unicode encodings use more than one byte per character to represent many characters.bytesobjects give you access to the underlying bytes.strobjects have theencodemethod that takes a string representing an encoding and returns thebytesobject that represents the string in that encoding.bytesobjects have thedecodemethod that takes a string representing an encoding and returns thestrthat results from interpreting thebyteas a string encoded in the the given encoding.For example:
We can see that UTF-8 is using four bytes,
\xce,\xb1,\xce, and\xac, to represent two characters.Related reading:
Python Unicode Howto (from the official documentation)
The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!) by Joel Spolsky
Pragmatic Unicode by Ned Batchelder