encode() - Returns an encoded version of the string

Python strings are stored as Unicode, which is a format that uses one byte for the characters between 0 and 128, and up to four bytes for other characters.  Unicode is the dominant encoding on the World Wide Web.  The encode method can be used to return the string with different encoding schemes.  A common encoding scheme is ASCII.

Example

Python
str = 'The quick brown fox jumped over the lazy dog'
binaryEncoded = str.encode()
print(binaryEncoded)

asciiEncoded = str.encode('ascii')
print(asciiEncoded)

Output

b'The quick brown fox jumped over the lazy dog'
b'The quick brown fox jumped over the lazy dog'

Notice how you can't tell what the encoding scheme is with the print function, but you get a clue that it's encoded because the string is prefaced with a "b" to indicate that the string is a sequence of octets -- 8 bit characters.

Notes

See https://docs.python.org/3/library/codecs.html#standard-encodings for all the supported encoding schemes.

You can tell the encode method how to handle errors, which includes:

  • strict - this is the default.  A UnicodeDecodeError exception is raised when the source string cannot be encoded.
  • ignore - ignores all unencodable Unicode characaters.
  • replace - replaces unencodable Unicode characters with a ?
  • xmlcharrefreplace - inserts XML character reference instead of the unencodable Unicode character.
  • backslashreplace - inserts a \uNNNN escape sequence instead of the unencodable Unicode.
  • namereplace - inserts a \N{...} escape sequence instead of the unencodable Unicode.

Example

Python
delta = '\u0394'
print(delta)
encoded = delta.encode('ascii', 'replace')
print(encoded)

Output

Δ
b'?'