The Byte Objects vs String in Python


As computers can only store bytes of data, we need to convert various data formats in byte data format. For example images to become bytes, are stored with PNG, JPEG etc. Similarly music is stored as .WAV, .MP3 etc. The software responsible for creating and managing this formats do the task of converting this data to bytes so that they can get stored. In python the byte object is a sequence of byte which is not human readable. But a character string is a sequence of characters which is human readable. A character gets encoded before it is stored in computer as bytes.

Encoding

Before the character string is stored into the disk, it has to be encoded. The function in python to encode the string is encode as shown below. Here we are applying the ASCII encoding.

Example

print('Best Tutorials'.encode('ASCII'))

Output

Running the above code gives us the following result −

b'Best Tutorials'

Decoding

When the bytes are read form the disk, to make them human readable, they need to be decoded. In python, we can use the decode function to convert the encoded bytes into strings.

Example

print(b'Best Tutorials'.decode('ASCII'))

Output

Running the above code gives us the following result −

Best Tutorials

Following are the key points to be noted.

  • Strings are sequence of character but Bytes objects are sequence of Bytes.
  • Strings are only in human readable but Bytes are machine readable.
  • Bytes are directly stored on the disk, whereas characters need encoding before getting stored in the disk.
raja
Published on 07-Aug-2019 08:01:45
Advertisements