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

Unlock This Lesson

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

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

bytearray Objects

Give Feedback

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:

>>>
>>> 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')

Become a Member to join the conversation.