How to Use bytearray() Function in PythonPython Basics


The bytearray() method returns a bytearray object which is a mutable sequence of integers in the range 0 <=x < 256.

Example

#!/usr/bin/python3
# -*- coding: UTF-8 -*-

print(bytearray([1,2,3]))
print(bytearray('ABCCBA', 'utf-8'))
print(bytearray())

Output:

bytearray(b'x01x02x03')
bytearray(b'ABCCBA')
bytearray(b'')

Syntax

bytearray([source[, encoding[, errors]]])

Parameters

Name Description
source Source to initialize the array of bytes.
encoding If the source is a string, the encoding of the string.
errors If the source is a string, the action to take when the encoding conversion fails.

Return Value

It returns an array of bytes of the given size.

© 2024 Cheerlearn.com, all rights reserved. Privacy Policy | Contact Us