Base de conhecimentoCentro de Exames
PT · PTPortuguês (Portugal)
Escolher idioma13

CKTIS / Python

bytearray()

Cria um vetor mutável de bytes.

Versão: Python 3.14; repository image, production runtime not attested

Sintaxe

bytearray()
bytearray(source)
bytearray(string, encoding, errors="strict")

Cria um vetor mutável de bytes.

Atribuir 65 à posição zero troca o a inicial por A, pelo que bytes(data) contém Abc. Com o inteiro 3 o construtor cria três bytes nulos.

Atribuir um valor fora de 0–255 gera ValueError.

data = bytearray(b"abc")
alias = data
data[0] = 65
assert bytes(alias) == b"Abc" and bytearray(3).hex() == "000000"

Exemplos

data = bytearray(b"abc")
data[0] = 65
print(bytes(data))
print(bytearray(3).hex())

Resultado

b'Abc'
000000

Atribuir um valor fora de 0–255 gera ValueError.

Fontes