>>> b' \x80 abc'.decode("utf-8 ","strict") Traceback (most recent call last): ... UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 0: invalid start byte >>> b' \x80 abc'.decode("utf-8 ","replace") '\ufffdabc' >>> b' \x80 abc'.decode("utf-8 ","backslashreplace ") '\\x80abc' >>> b' \x80 abc'.decode("utf-8 ","ignore") 'abc' Encodings are specified as strings containing the encoding’s name. Python comes with roughly 100 different encod- ings; see the Python Library Reference at standard-encodings for a list. Some encodings have multiple names; for example,$_{'latin-1'}$ ,$_{'iso_8859_1'}$ and$_{'8859}$ ’ are all synonyms for the same encoding. One-character Unicode strings can also be created with the$_{chr()}$ built-in function, which takes integers and returns a Unicode string of length 1 that contains the corresponding code point. The reverse operation is the built-in$_{ord()}$ function that takes a one-character Unicode string and returns the code point value: The opposite method of$_{bytes.decode()}$ is$_{str.encode()}$ , which returns a$_{bytes}$ representation of the Unicode string, encoded in the requested encoding . The errors parameter is the same as the parameter of the$_{decode()}$ method but supports a few more possible handlers. As well as$_{'strict'}$ ,$_{'ignore'}$ , and$_{'replace'}$ (which in this case inserts a question mark in- stead of the unencodable character), there is also$_{'xmlcharrefreplace'}$ (inserts an XML character reference), backslashreplace (inserts a$_{\uNNNN}$ escape sequence) and$_{namereplace}$ (inserts a$_{\N{...}}$ escape se- quence). The following example shows the different results: The low-level routines for registering and accessing the available encodings are found in the$_{codecs}$module. Im- plementing new encodings also requires understanding the$_{codecs}$module. However, the encoding and decoding functions returned by this module are usually more low-level than is comfortable, and writing new encodings is a specialized task, so the module won’t be covered in this HOWTO. 5 >>> u =chr(40960 )+'abcd'+chr(1972 ) >>> u.encode('utf-8 ') b'\xea\x80\x80abcd\xde\xb4' >>> u.encode('ascii') Traceback (most recent call last): ... UnicodeEncodeError: 'ascii' codec can't encode character '\ua000' in position 0: ordinal not in range(128) >>> u.encode('ascii','ignore') b'abcd' >>> u.encode('ascii','replace') b'?abcd?' >>> u.encode('ascii','xmlcharrefreplace ') b'&#40960;abcd&#1972;' >>> u.encode('ascii','backslashreplace ') b'\\ua000abcd\\u07b4' >>> u.encode('ascii','namereplace') b'\\N{YI SYLLABLE IT}abcd\\u07b4' >>> chr(57344 ) '\ue000' >>> ord(' \ue000 ') 57344 2.2 Converting to Bytes