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. In this article, we will see the difference between byte objects and strings in Python and also will look at how we can convert byte string to normal string and vice versa.
Byte Objects vs String in Python
|
S.NO |
Byte Object |
Python String |
|---|---|---|
|
1. |
Byte objects are a sequence of Bytes. |
Strings are a sequence of characters. |
|
2. |
Byte objects are in machine-readable form internally. |
Strings are only in human-readable form. |
|
3. |
Bytes are used to handle binary data. |
Strings are used to handle textual data. |
|
4. |
They can be directly stored on the disk. |
They need encoding before they are stored on disk. |

Convert 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.
Python3
# Python code to demonstrate String encoding# initialising a String a = 'GeeksforGeeks'# initialising a byte objectc = b'GeeksforGeeks'# using encode() to encode the Stringd = a.encode('ASCII')# checking if a is converted to bytes or notif (d==c): print ("Encoding successful")else : print ("Encoding Unsuccessful") |
Encoding successful
Convert Byte Objects to String (Decoding)
Similarly, Decoding is process to convert a Byte object to String. It is implemented using decode() . A byte string can be decoded back into a character string, if you know which encoding was used to encode it. Encoding and Decoding are inverse processes.
Python3
# Python code to demonstrate Byte Decoding# initialising a String a = 'GeeksforGeeks'# initialising a byte objectc = b'GeeksforGeeks'# using decode() to decode the Byte objectd = c.decode('ASCII')# checking if c is converted to String or notif (d==a): print ("Decoding successful")else : print ("Decoding Unsuccessful") |
Decoding successful
Concatenating a String and a Byte String
In this example, we have defined a string “Geeks” and a byte string “forGeeks”. After that we used decode() method to convert byte string to normal string and then concatenate both of them.
Python3
# Define a stringstring = "Geeks"# Define a byte stringbyte_str = b"forGeeks"# Concatenate the string and byte stringans = string + byte_str.decode()print(ans) |
GeeksforGeeks
Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills and become a part of the hottest trend in the 21st century.
Dive into the future of technology - explore the Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.
