Python bytearray() Function
Description
bytearray() The method returns a new byte array. The elements in this array are mutable, and the value of each element is in the range: 0 <= x < 256.
Syntax
bytearray() Method Syntax:
class bytearray([source[, encoding[, errors]]])
Parameters
- If source is an integer, an initialized array of length source is returned;
- If source is a string, the string is converted into a byte sequence according to the specified encoding;
- If source is an iterable type, its elements must be integers in [0, 255];
- If source is an object consistent with the buffer interface, this object can also be used to initialize bytearray.
- If no parameters are provided, by default an array with 0 elements is initialized.
Return Value
Returns a new byte array.
Example
The following examples show the use of bytearray():
bytearray
(
)
bytearray
(
b
'
'
)
bytearray
(
[
1
,
2
,
3
]
)
bytearray
(
b
'
\x
01
\x
02
\x
03
'
)
bytearray
(
'
runoob
'
,
'
utf-8
'
)
bytearray
(
b
'
runoob
'
)
Python built-in function