Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

bytearray Objects

In this lesson, you’ll explore bytearray objects. bytearray objects are very similar to bytes objects, despite these differences:

  • There is no dedicated syntax for defining a bytearray literal.
  • A bytearray is always created using the bytearray() built-in function.
  • bytearray objects are mutable.

Here’s an example:

Python
>>> ba = bytearray('spam.egg.bacon', 'utf8')
>>> ba
bytearray(b'spam.egg.bacon')
>>> type(ba)
<class 'bytearray'>

>>> ba2 = bytearray(6)
>>> ba2
bytearray(b'\x00\x00\x00\x00\x00\x00')

>>> ba3 = bytearray([97, 98, 99, 100, 101])
>>> ba3
bytearray(b'abcde')

>>> ba3[4] = 0xee
>>> ba3
bytearray(b'abcd\xee')

>>> ba3[:3] = b'egg'
>>> ba3
bytearray(b'eggd\xee')

>>> ba4 = bytearray(b'spam')
>>> ba4
bytearray(b'spam')

00:00 For this video, I’m going to show you bytearray objects. Python supports another binary sequence type called the bytearray. bytearray objects are very much like bytes objects, despite a couple of differences.

00:15 There isn’t any dedicated syntax for defining a bytearray literal. You can’t use the letter b in the front of a string in the same way that you could with the bytes objects.

00:28 A bytearray is always created using the built-in bytearray() function. And one other large difference is bytearray objects are mutable, unlike bytes objects, so you can use indexing and slicing to change the contents.

00:42 Let me show you some examples. So again, you can’t use the b prefix in front of your string to create a bytearray. A bytearray object, like this one called ba, is always created with the bytearray() function.

00:58 And in this case—let’s say you start with a string—here, you’re going to put in the same kind of encoding that you did before.

01:09 In this case, I used 'utf8'. And you’ll notice that ba isn’t prefixed with the b at the front of it—it’s actually bytearray(). And if you were to do type(ba), it would definitely tell you that it’s of the <class 'bytearray'>. Make another new bytearray called ba2. For this one, like you did when creating a bytes object, enter an integer, and it will in the same way create a bytearray with null-value bytes inside of it.

01:42 You can also create a bytearray using an iterable. Give it a list of integers,

01:56 and here you can see those bytes displayed as ASCII characters. One kind of unique thing that you could do here is take ba3, the index of the last character—in this case, 4and reassign it to something else.

02:13 Let’s say ee hexadecimal. Now ba3 looks a little different. The letter 'e' has been replaced by this byte value instead. So there, you can see the mutability. If you were to make a slice, say from the beginning to 3 and change it

02:37 to say 'egg' instead, you can enter it in as a byte literal. Now ba3 starts with b'egg'. And one last thing—a bytearray can be constructed from bytes objects as well.

02:52 If you put in a byte literal—and in that case, since they are already bytes, you don’t need to put an encoding type—that’ll create a bytearray also.

03:07 So, many similarities and a couple of differences between bytearray and the bytes object. Let’s wrap this whole thing up and I’ll take you through a review of everything you’ve learned.

Avatar image for Adam Masiarek

Adam Masiarek on June 19, 2021

ba = bytearray('spam.egg.bacon', 'utf8')

print(str(ba)) # bytearray(b'spam.egg.bacon'),   how to get string?
Avatar image for Adam Masiarek

Adam Masiarek on June 19, 2021

Google friend:

print(ba.decode())
Avatar image for VitaminC

VitaminC on Feb. 24, 2022

What is the normal use case of byte objects? Don’t understand where they might be used in practice.

Avatar image for Bartosz Zaczyński

Bartosz Zaczyński RP Team on Feb. 24, 2022

@VitaminC Bytes are useful anytime you want to interface with binary data such as still images, videos, or audio, for example. The bitwise operator tutorial showcases several uses of the built-in bytes() data type, including concealing information in a bitmap image.

Become a Member to join the conversation.