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 str
represents one unicode character. However to represent more than 256 characters, individual unicode encodings use more than one byte per character to represent many characters. bytearray
objects give you access to the underlaying bytes. str
objects have the encode
method that takes a string representing an encoding and returns the bytearray
object that represents the string in that encoding. bytearray
objects have the decode
method that takes a string representing an encoding and returns the str
that results from interpreting the bytearray
as a string encoded in the the given encoding. Here's an example.
>>> a = "α?".encode('utf-8')
>>> a
b'xcexb1xcexac'
>>> a.decode('utf-8')
'α?'
We can see that UTF-8 is using four bytes, xce, xb1, xce, and xac to represent two characters. After the Spolsky article that Ignacio Vazquez-Abrams referred to, I would read the Python Unicode Howto.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…