How To Create, Modify, Append ByteArray Object In Python

In this article, I will tell you what is python ByteArray, how to create or modify a python ByteArray object, how to append data to a python ByteArray object with examples.

1. What Is Python ByteArray.

  1. In Python, bytearray is a mutable sequence of bytes. It is similar to a list, but each element in a bytearray is an integer between 0 and 255, representing a single byte of data.
  2. One of the main benefits of bytearray over other Python data types is that it is mutable.
  3. This means that you can change the values of individual elements in the bytearray after it has been created.
  4. This can be useful in situations where you need to modify data that is represented as a sequence of bytes, such as in network communication or file I/O.

2. How To Create Python ByteArray Object Examples.

  1. In Python, the bytearray() function is used to create a mutable sequence of bytes. The function can take one of the following arguments:
  2. If no argument is provided, an empty bytearray object is created.
  3. If an integer argument is provided, a bytearray object of the specified length is created, with all elements initialized to zero.
  4. If an iterable argument is provided, a bytearray object is created with the same elements as the iterable.

2.1 Example 1.

  1. A bytearray object can be created by calling the bytearray() function with an optional argument that specifies the length of the bytearray or an iterable that contains the elements of the bytearray.
  2. Here is an example of how to create and use a bytearray object in Python:
    # Create a bytearray object from a string
    my_string = "hello world"
    my_bytearray = bytearray(my_string, "utf-8")
    
    # Print the bytearray object
    print(my_bytearray)   # Output: bytearray(b'hello world')
    # Modify an element of the bytearray object
    my_bytearray[0] = 72   # Change the first element to the ASCII code for 'H'
    
    # Print the modified bytearray object
    print(my_bytearray)   # Output: bytearray(b'Hello world')
  3. In this example, a bytearray object is created from a string using the bytearray() function with the second argument "utf-8".
  4. The bytearray object is then modified by changing the first element to the ASCII code for ‘H’, and the modified bytearray object is printed to the console.

2.2 Example 2.

  1. Creating an empty bytearray object.
    my_bytearray = bytearray()
    
    print(my_bytearray)   # Output: bytearray(b'')
    
    print('bytearray length is : ' + str(len(my_bytearray)))
  2. When you run the above code you will get the below output in console.
    bytearray(b'')
    bytearray length is : 0

2.3 Example 3.

  1. Creating a bytearray object with a specified length.
    my_bytearray = bytearray(5)
    
    print(my_bytearray)   # Output: bytearray(b'\x00\x00\x00\x00\x00')
    
    print('bytearray length is : ' + str(len(my_bytearray)))
  2. Below is the output in the console when you run the above python source code.
    bytearray(b'\x00\x00\x00\x00\x00')
    bytearray length is : 5
  3. In this example, a bytearray object of length 5 is created, with all elements initialized to zero.

2.4 Example 4.

  1. Creating a bytearray object from an iterable.
    my_list = [97, 98, 99, 100]
    
    my_bytearray = bytearray(my_list)
    
    print(my_bytearray)   # Output: bytearray(b'abcd')
  2. Below is the output in the console.
    bytearray(b'abcd')
  3. In this example, a bytearray object is created from a list of integers. The integers are converted to their corresponding ASCII characters and added to the bytearray object.

3. How To Modify A Python ByteArray Object.

  1. You can also modify a bytearray object, since it is mutable.
  2. Here is an example of how to modify a bytearray object:
    my_bytearray = bytearray(b"hello")
    
    print("Before modify : ")
    
    print(my_bytearray)     # Output: bytearray(b'dello')
    
    my_bytearray[0] = 100   # Change the first element to from 'h' to 'd' with the new ascii code.
    
    print("After modify : ")
    
    print(my_bytearray)     # Output: bytearray(b'dello')
  3. Below is the above python code output.
    Before modify : 
    bytearray(b'hello')
    After modify : 
    bytearray(b'dello')
  4. In this example, the first element of the bytearray object is changed from the ASCII code for ‘h’ to the ASCII code for ‘d’.

4. Python ByteArray Append Example.

4.1 Use append() Method Example.

  1. In Python, you can append elements to a bytearray object using the append() method.
  2. The append() method adds a single element to the end of the bytearray.
  3. Here’s an example of how to use the append() method to add elements to a bytearray object:
    # Create an empty bytearray
    my_bytearray = bytearray()
    # Append elements to the bytearray
    my_bytearray.append(72)   # Append the ASCII code for 'H'
    my_bytearray.append(101)  # Append the ASCII code for 'e'
    my_bytearray.append(108)  # Append the ASCII code for 'l'
    my_bytearray.append(108)  # Append the ASCII code for 'l'
    my_bytearray.append(111)  # Append the ASCII code for 'o'
    # Print the final bytearray
    print(my_bytearray)   # Output: bytearray(b'Hello')
  4. In this example, an empty bytearray is created using the bytearray() function.
  5. The append() method is then called multiple times to add elements to the end of the bytearray, representing the characters in the string “Hello”.
  6. Finally, the completed bytearray is printed to the console.

4.2 Use extend() Method Example.

  1. You can also append multiple elements to a bytearray using the extend() method.
  2. The extend() method takes an iterable as an argument, and adds each element of the iterable to the end of the bytearray.
  3. Here’s an example of how to use the extend() method:
    # Create an empty bytearray
    my_bytearray = bytearray()
    # Append multiple elements to the bytearray
    my_bytearray.extend([72, 101, 108, 108, 111])
    # Print the final bytearray
    print(my_bytearray)   # Output: bytearray(b'Hello')
  4. In this example, an empty bytearray is created using the bytearray() function.
  5. The extend() method is then called with a list of integers representing the characters in the string “Hello”.
  6. Each element of the list is added to the end of the bytearray, and the completed bytearray is printed to the console

References

  1. How To Convert A Python Bytearray Object To String, Bytes, Int, List With Examples

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.