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, Strings need encoding before which they can be stored on disk.
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.
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.
This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- How to generate byte code file in python ?
- Python - Interconvert Tuple to Byte Integer
- Reading Python File-Like Objects from C | Python
- File Objects in Python
- Timer Objects in Python
- Barrier Objects in Python
- Python | Counter Objects | elements()
- Mutable vs Immutable Objects in Python
- Python Tkinter | Moving objects using Canvas.move() method
- Python | Unit Test Objects Patching | Set-1
- Python | Unit Test Objects Patching | Set-2
- Python Classes and Objects
- Python objects
- Print objects of a class in Python
- Python: Difference between Lock and Rlock objects
- Flattening JSON objects in Python
- Session Objects - Python requests
- Built-in Objects in Python-builtins
- Python del to delete objects
- Python - Edit objects inside tuple