Bytes() Function In Python

Function Name:

bytes

 

Function Signature:

class bytes([source[, encoding[, errors]]])

 

Function Overview:

  • The function bytes, is a constructor to the class bytes.

 

  • The bytes is a collection of bytes. Elements of bytes take values from 0 to 255.

 

  • bytes is just similar to bytesarray. However bytes is an immutable sequence.

Example:

 

# Create a null bytes array

BYTES_LENGTH    = 8

myBytes         = bytes(BYTES_LENGTH)

 

#print null bytes...cant do anything else as bytes is immutable

print(myBytes)

 

# Create bytes from strings

sampleString = "Hello World"

bytes1       = bytes(sampleString, "utf-8")

print(bytes1)

 

Output:

 

b'\x00\x00\x00\x00\x00\x00\x00\x00'

b'Hello World'


Copyright 2019 © pythontic.com