Python bytes () function

Python Methods and Functions

Python bytes () &a built-in function that returns a byte object that is an immutable sequence of integers in the range 0 & lt; = x & lt; 256. Depending on the type of object passed as the source, it initializes the byte object accordingly.




Syntax

Takes three optional parameters, namely:

  • source - & gt; The source that initializes the byte array
  • encoding - & gt;  source string encoding (can be UTF-8, etc.).
  • errors - & gt; Function behavior when the encoding of the source string fails.
 byte_array = bytes (source, encoding, errors) 

Since all three arguments are optional, we can pass an empty string to generate an empty byte array (a byte array of size 0).


< / ins>

Depending on the type of source parameter, the corresponding byte array will be initialized.

  • If source is a string, Python bytes () converts the string to bytes using str.encode () . Hence, we also have to provide encoding and possibly errors, since encode () is used to process the string.
  • If source is an integer, bytes () will create an array of the given integer size, all initialized to NULL.
  • If source is of class Object , the buffer is Object read-only will be used to initialize the byte array.
  • If source is iterable, it must be an iterative integer in the range 0 & lt; = x & lt; 256, which are used as the initial contents of the array.

If source & None , this will result in a TypeError as it cannot convert the None object to a byte array.

To better understand the function , let`s see some examples.




No arguments

 b = bytes () print (b) c = bytes (None ) print (c) 

Exit

 b`` TypeError: cannot convert `NoneType` object to bytes  


With original string

Any string provided without an encoding will raise a TypeError

Similarly, trying to modify the bytes object will also result in the same exception, since it is immutable in nature.



try: a = bytes (`Hello from AskPython`) except TypeError: print (` We need to specify string encoding always! `) b = bytes (` Hello from AskPython`, `UTF-8`) print (type (b), b) try: b [0] = 10 except TypeErro r: print (` byte objects are immutable! ` )

Exit

 We need to specify string encoding always! & lt; class `bytes` & gt; b`Hello from AskPython` byte objects are immutable! 



With an original integer

An integer initializes this number of byte element objects in the array.

 a = bytes (10) print (type (a), a) 

Exit

 & lt; class `bytes` & gt; b`x00x00x00x00x00x00x00x00x00x00` 

As you can see, the bytes object is a zero-initialized array of 10 elements.




With an iterative source

This initializes the array with len (iterable) the number of elements, each of which has a value equal to the corresponding element in the iterable.

The values ​​of byte arrays can be accessed by regular iteration, but they cannot be changed because they are are immutable.

 a = bytes ([1, 2, 3]) print (type (a), a) print (`Length =` , len (a)) # To access the byte array values, we can iterate through it! for byte_obj in a: print (byte_object) 

Exit

 & lt; class `bytes` & gt; b`x01x02x03` Length = 3 1 2 3 

Anything else on Iterable will result in TypeError

 & gt; & gt; & gt; a = bytes ([1, 2, 3, `Hi`]) Traceback (most recent call last): File" & lt; stdin & gt; ", line 1, in & lt; module & gt; TypeError: `str` object cannot be interpreted as an integer 







Get Solution for free from DataCamp guru