Byte Objects Vs String in Python
Byte Objects Vs String in Python
In Python 2, both str and bytes are the same typeByte objects whereas in Python 3 Byte objects,
defined in Python 3 are “sequence of bytes” and similar to “unicode” objects from Python 2.
However, there are many differences in strings and Byte objects. Some of them are depicted
below:
`
Byte objects are sequence of Bytes, whereas Strings are sequence of characters.
Byte objects are in machine readable form internally, Strings are only in human
readable form.
Since Byte objects are machine readable, they can be directly stored on the disk.
Whereas, Stringsneed encoding before which they can be stored on disk.
There are methods to convert a byte object to String and String to byte objects.
Encoding
PNG, JPEG, MP3, WAV, ASCII, UTF-8 etc are different forms of encodings. An encoding is a format
to represent audio, images, text, etc in bytes. Converting Strings to byte objects is termed as
encoding. This is necessary so that the text can be stored on disk using mapping
using ASCII or UTF-8 encoding techniques.
This task is achieved using encode(). It take encoding technique as argument. Default
technique is “UTF-8” technique.
# Python code to demonstate String encoding
# initialising a String
a = 'DrinkandDrive'
# initialising a byte object
c = b'DrinkandDrive'
# using encode() to encode the String
d = a.encode('ASCII')
# checking if a is converted to bytes or not
if (d==c):