liveBook · Manning

Encoding

As explained in The Quick Python Book, Fourth Edition

encode is a string method that converts text into a bytes object using a character encoding.

Encoding converts a Unicode string into a bytes object. Strings contain Unicode characters; bytes contain integer values, so the two types may look similar but cannot be used interchangeably. Use decode to convert bytes back into a string. section 6.9

Keep these distinctions in mind:

  • Encoded text may occupy multiple bytes and no longer print like the original character. section 6.9
  • A bytes object cannot be concatenated directly with a string; convert one type before combining them. section 6.9
  • UTF-8 preserves basic ASCII characters while supporting a much wider range of alphabets and symbols. section 21.2.1 section 21.7
  • Choosing ASCII for broader text can produce decoding errors when files contain accented or otherwise non-ASCII characters. section 21.2.1

Encode and decode an accented character

unicode_a_with_acute = '\N{LATIN SMALL LETTER A WITH ACUTE}' xb = unicode_a_with_acute.encode() # xb prints as b'\xc3\xa1' xb.decode() # 'á' xb += 'A' # TypeError: can't concat str to bytes

unicode_a_with_acute = '\N{LATIN SMALL LETTER A WITH ACUTE}'
unicode_a_with_acute
 
'á'
 
xb = unicode_a_with_acute.encode()           #1
xb
 
b'\xc3\xa1'                                  #2
 
xb += 'A'                                    #3
 
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-93-2c4ecee004ef> in <cell line: 1>()
----> 1 xb += 'A'
 
TypeError: can't concat str to bytes
 
xb.decode()                                  #4
 
'á'
#1 The encode method converts string to bytes.
#2 The encoded bytes object is 2 bytes; it doesn’t print as a character.
#3 bytes string objects are different types.
#4 The object’s decode method will convert it back to a string.

Inspect the complete runnable example showing the original Unicode character, its encoded bytes, the failed mixed-type concatenation, and the decoded result. section 6.9

How The Quick Python Book, Fourth Edition covers it

How deep it goes
Explains string-to-bytes conversion, reverse decoding, type incompatibility, and ASCII-versus-UTF-8 pitfalls without detailing codec internals.
Examples
Uses an accented character, displayed encoded bytes, failed mixed-type concatenation, and text-file decoding errors.
What you can do after
Apply it via string and bytes methods to convert text, reverse the conversion, and recognize incompatible types or encoding errors.
How it connects
Connects encoding with strings, bytes, Unicode, UTF-8, ASCII, decoding, binary data, and text files.

Also explained in

Compare how 9 books explain encoding →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Quick Python Book, Fourth Edition. The section links point to the parts of the book it draws on.