Skip to main content

Python bytearray() Function

The bytearray() function returns a mutable bytearray object.

It can convert objects into bytearray objects, or create empty bytearray object of the specified size.

note

The difference between bytes() and bytearray() is that bytes() returns an object that can not be modified, and bytearray() returns an object that can be modified.

Syntax

bytearray(x, encoding, error)

bytearray() Parameters

Python bytearray() function parameters:

ParameterConditionDescription
xOptionalA source to use when creating the bytearray object.
If it is an integer, an empty bytearray object of the specified size will be created.
If it is a String, make sure you specify the encoding of the source.
encodingOptionalThe encoding of the string like ascii, utf-8, windows-1250, windows-1252, etc.
errorOptionalSpecifies what to do if the encoding fails (strict, replace, ignore, backslashreplace)
note

Notice that different error parameter has different effects:

  • strict will raise an exception in case of an encoding error
  • replace will replace malformed data with a suitable replacement marker, such as ‘?’ or ‘ufffd’
  • ignore will ignore malformed data and continue without further notice
  • xmlcharrefreplace will replace with the appropriate XML character reference (for encoding only)
  • backslashreplace will replace with backslashed escape sequences (for encoding only)

bytearray() Return Value

Python bytearray() method returns an array of bytes of the given size and initialization values.

Examples

Example 1: Basic examples

The following example shows various ways to create bytearray objects:

print(bytearray(b''))       # Output: bytearray(b'')
print(bytearray(4)) # Output: bytearray(b'\x00\x00\x00\x00')
print(bytearray([0,1,2])) # Output: bytearray(b'\x00\x01\x02')

output

bytearray(b'')
bytearray(b'\x00\x00\x00\x00')
bytearray(b'\x00\x01\x02')

Example 2: array of bytes from a string

Let's convert a string into a bytearray while specifying the encoding.

string = "This is Tutorial Reference!"

# string with encoding 'utf-8'
arr = bytearray(string, 'utf-8')
print(arr)

output

bytearray(b'This is Tutorial Reference!')

Example 3: array of bytes of given integer size

Let's create a bytearray of a specific size using an integer value:

size = 5

arr = bytearray(size)
print(arr)

output

bytearray(b'\x00\x00\x00\x00\x00')

Example 4: array of bytes from an iterable list

Let's convert an iterable list of integers into a bytearray:

rList = [1, 2, 3, 4, 5]

arr = bytearray(rList)
print(arr)

output

bytearray(b'\x01\x02\x03\x04\x05')

Examples using also encoding and error parameters

See some examples of what happens with encoding as ascii and the various errors

Example:

print(bytearray(u'źdźbło', 'ascii', 'strict'))
Traceback (most recent call last):
File "main.py", line 1, in <module>
print(bytearray(u'źdźbło', 'ascii', 'strict'))
UnicodeEncodeError: 'ascii' codec can't encode character '\u017a' in position 0: ordinal not in range(128)

Example:

print(bytearray(u'źdźbło', 'ascii', 'ignore'))
bytearray(b'dbo')

Example:

print(bytearray(u'źdźbło', 'ascii', 'replace'))
bytearray(b'?d?b?o')

Example:

print(bytearray(u'źdźbło', 'ascii', 'xmlcharrefreplace'))
bytearray(b'&#378;d&#378;b&#322;o')

Example:

print(bytearray(u'źdźbło', 'ascii', 'backslashreplace'))
bytearray(b'\\u017ad\\u017ab\\u0142o')