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.
- In Python,
bytearray
is a mutable sequence of bytes. It is similar to a list, but each element in abytearray
is an integer between 0 and 255, representing a single byte of data. - One of the main benefits of
bytearray
over other Python data types is that it is mutable. - This means that you can change the values of individual elements in the
bytearray
after it has been created. - 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.
- In Python, the
bytearray()
function is used to create a mutable sequence of bytes. The function can take one of the following arguments: - If no argument is provided, an empty
bytearray
object is created. - If an integer argument is provided, a
bytearray
object of the specified length is created, with all elements initialized to zero. - If an iterable argument is provided, a
bytearray
object is created with the same elements as the iterable.
2.1 Example 1.
- A
bytearray
object can be created by calling thebytearray()
function with an optional argument that specifies the length of thebytearray
or an iterable that contains the elements of thebytearray
. - 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')
- In this example, a
bytearray
object is created from a string using thebytearray()
function with the second argument"utf-8"
. - The
bytearray
object is then modified by changing the first element to the ASCII code for ‘H’, and the modifiedbytearray
object is printed to the console.
2.2 Example 2.
- Creating an empty
bytearray
object.my_bytearray = bytearray() print(my_bytearray) # Output: bytearray(b'') print('bytearray length is : ' + str(len(my_bytearray)))
- When you run the above code you will get the below output in console.
bytearray(b'') bytearray length is : 0
2.3 Example 3.
- 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)))
- 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
- In this example, a
bytearray
object of length 5 is created, with all elements initialized to zero.
2.4 Example 4.
- 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')
- Below is the output in the console.
bytearray(b'abcd')
- 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 thebytearray
object.
3. How To Modify A Python ByteArray Object.
- You can also modify a
bytearray
object, since it is mutable. - 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')
- Below is the above python code output.
Before modify : bytearray(b'hello') After modify : bytearray(b'dello')
- 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.
- In Python, you can append elements to a bytearray object using the append() method.
- The append() method adds a single element to the end of the bytearray.
- 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')
- In this example, an empty
bytearray
is created using thebytearray()
function. - The
append()
method is then called multiple times to add elements to the end of thebytearray
, representing the characters in the string “Hello”. - Finally, the completed
bytearray
is printed to the console.
4.2 Use extend() Method Example.
- You can also append multiple elements to a
bytearray
using theextend()
method. - The
extend()
method takes an iterable as an argument, and adds each element of the iterable to the end of thebytearray
. - 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')
- In this example, an empty
bytearray
is created using thebytearray()
function. - The
extend()
method is then called with a list of integers representing the characters in the string “Hello”. - Each element of the list is added to the end of the
bytearray
, and the completedbytearray
is printed to the console
References