codescracker
python

bytearray in Python



« Python Tutorial Python Examples »

bytearray in Python is a binary sequence type. There are 3 binary sequence types available in Python:

  1. bytes
  2. bytearray
  3. memoryview

The bytearray is the core inbuilt type used in manipulating the binary data. Unlike bytes, bytearray objects are mutable or changeable.

Creating bytearray in Python

A bytearray object can be created in four ways. All those fours ways of creating a bytearray object in Python are given below.

Using bytearray() Constructor

The bytearray() constructor is always required to create a bytearray object in Python. Here is an example:

x = bytearray()
print(x)
print(type(x))

The snapshot given below shows the sample output produced by this Python program, showing the value and type of variable x:

python bytearray

Creating bytearray of Given Length

Here is another example of creating a bytearray object with specified length:

x = bytearray(5)
print(x)
print(type(x))

Following is the output produced by this Python code:

bytearray(b'\x00\x00\x00\x00\x00')
<class 'bytearray'>

Creating bytearray from an Iterable

Here is the third example of creating a bytearray from a iterable:

x = bytearray(range(5))
print(x)
print(type(x))

The output would be:

bytearray(b'\x00\x01\x02\x03\x04')
<class 'bytearray'>

Copying an Existing Binary Data via Buffer Protocol

Here is the last example of creating a bytearray object by copying an existing binary data via the buffer protocol:

x = bytearray(b'codescracker dot com')
print(x)
print(type(x))

The output would be:

bytearray(b'codescracker dot com')
<class 'bytearray'>

Python bytearray Example

This program is created to allow user to enter a string. The entered string gets converted into bytearray object using the bytearray() and with the utf-8 and utf-16 encoding. You can try some other encoding too, with yourself.

print("Enter the String: ", end="")
str = input()

print("\nString converted into bytearray object with UTF-8 encoding:")
ba = bytearray(str, "UTF-8")
print(ba)

print("\nString converted into bytearray object with UTF-16 encoding:")
ba = bytearray(str, "UTF-16")
print(ba)

The snapshot given below shows the sample run with user input python:

python bytearray example

Python bytearray Objects are Mutable

Because bytearray objects are mutable, therefore we can modify the bytes object anytime in the same program. Here is an example:

str = "codescracker dot com"
x = bytearray(str, "utf-8")
print(x)

del x[12:]
print(x)

x[12:] = b".com"
print(x)

In above program, using the statement:

del x[12:]

all characters from index number 12th (included) to last, will get deleted. And using the following statement:

x[12:] = b".com"

the new value, .com will get appended/replaced from the 12th index. Here is the sample output produced by above Python program:

bytearray(b'codescracker dot com')
bytearray(b'codescracker')
bytearray(b'codescracker.com')

Note - To learn about slicing in detail, refer to Python List. There, you'll get all the detail.

To convert bytearray object to a string object, refer to Python bytearray to string program article.

Python Online Test


« Python Tutorial Python Examples »



© Copyright 2021. All Rights Reserved.

CodesCracker