Thursday, July 1, 2021

Python for Beginners: Bytes in Python

You must have learnt about different data types in python such as strings and numeric data types like integers and floating point numbers. In this article you will learn about another data type called bytes. You will study the underlying concepts behind bytes in python and will implement different types of operations on bytes to understand the concepts.

What are bytes in Python?

Generally when we save any data in secondary storage, it is encoded according to a certain type of encoding such as ASCII, UTF-8 and UTF-16 for strings, PNG,JPG and JPEG for images and mp3 and wav for audio files and is turned into a byte object. When we access the data again using python read file operation, it is decoded into the corresponding text, image or audio. Byte objects contain data that are machine readable and we can store a byte object directly into secondary storage. 

In python, we can explicitly create byte objects from other data such as lists, strings etc.

How to create bytes in Python?

To create byte objects we can use the bytes() function. The bytes() function takes three parameters as input all of which are optional. The object which has to be converted to bytes is passed as the first parameter. Second and third parameters are used only when the first parameter is  string. In this case, the second parameter is the encoding of the string and the third parameter is the name of the error response which is executed when the encoding fails. The bytes() function returns an immutable byte object. In the next sections, we will understand the working of bytes() function by creating bytes objects from different data objects.

Create a bytes object of given size

To create a bytes object of any given size, we will pass the size as input to the bytes() method and a bytes object of the required size is created which is initialized to all zeros. This can be understood from the following example.

bytes_obj = bytes(10)
print("The bytes object is:", bytes_obj)
print("Size of the bytes object is:", len(bytes_obj) )

Output:

The bytes object is: b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
Size of the bytes object is: 10

Convert String to bytes

To convert a string to bytes object, we will pass the string as first input and encoding as the second input to the bytes() function. There is also a third argument for error response but can be avoided for simplicity at this time. The function returns a bytes object with the encoded string. This can be understood as follows.

myString = "Pythonforbeginners.com"
print("The given string is:" , myString)
bytes_obj = bytes(myString , "UTF-8")
print("The bytes object is:", bytes_obj)
print("Size of the bytes object is:", len(bytes_obj) )

Output:

The given string is: Pythonforbeginners.com
The bytes object is: b'Pythonforbeginners.com'
Size of the bytes object is: 22

Convert a list to bytes

We can also convert any iterable object like list or tuple to bytes object using bytes() function.To perform this operation, we simply pass the iterable object to the bytes() function which returns the corresponding bytes object.Remember that bytes object is immutable and cannot be modified. We can convert a list into bytes using bytes() function as follows.

myList = [1,2,3,4,5]
print("The given list is:" , myList)
bytes_obj = bytes(myList)
print("The bytes object is:", bytes_obj)
print("Size of the bytes object is:", len(bytes_obj) )

Output:

The given list is: [1, 2, 3, 4, 5]
The bytes object is: b'\x01\x02\x03\x04\x05'
Size of the bytes object is: 5

Remember that the list passed to bytes() function should only contain elements. Passing s list with floating point numbers or strings will cause bytes() function to throw TypeError.

Conclusion

In this article, we have seen what bytes objects are and how we can create bytes objects from iterables and strings using bytes() method.We can also write the programs used in this article with exception handling using python try except to make the programs more robust and handle errors in a systematic way. Stay tuned for more informative articles.

The post Bytes in Python appeared first on PythonForBeginners.com.



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...