In Python, encoding is the process of converting a string or a text file to bytes. It helps in representing the textual data in a form that can be easily stored and transmitted. Different encoding formats like ASCII, Unicode, UTF-8, etc., are used for different languages.

Understanding Encoding

Encoding a string makes it easy to save the data to a file or send it over a network. When a text file gets encoded, each character of the file gets replaced with a specific pattern of bits. It’s similar to a translation from a human-readable format to a machine-readable one.

Types of Python Encoding


ASCII (American Standard Code for Information Interchange) was one of the first encoding systems. It describes a set of 128 characters, including Latin letters, numerals, and special symbols, each represented with 7 bits.

1word = "Hello"
2ascii_encoded_word = word.encode('ASCII')

This will output: b'Hello', where the prefix b indicates it’s a bytes sequence.

Though ASCII is simple, it’s not designed to handle non-English characters or symbols beyond its set.


To overcome the limitations of ASCII, Unicode was introduced. It’s designed to represent text in any writing system or language globally. Unicode uses two bytes (16 bits) for each character, encompassing over 65,000 unique characters.


UTF-8 (8-bit Unicode Transformation Format) is a variable-length encoding system for Unicode, capable of representing any character in the Unicode standard. It has become the dominant character encoding for the web as it can handle any Unicode character, yet it remains backward compatible with ASCII.

1word = "こんにちは"
2utf8_encoded_word = word.encode('UTF-8')

This will output: b'\xe3\x81\x93\xe3\x82\x93\xe3\x81\xab\xe3\x81\xa1\xe3\x81\xaf'.

Decoding in Python

Decoding is the reverse process of encoding, which converts the bytes back to the original string.

1original_word = utf8_encoded_word.decode('UTF-8')


Always ensure to use the same encoding to decode which was used for encoding.

Understanding encoding is significant, particularly when dealing with different languages other than English, textual data processing, and when working with communication between different software or protocols.