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.
Takes three optional parameters, namely:
source- & gt; The source that initializes the byte array
encoding- & gt;
sourcestring 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).
Depending on the type of
source parameter, the corresponding byte array will be initialized.
source code > 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.
sourceis an integer, bytes () will create an array of the given integer size, all initialized to NULL.
sourceis of class
Object, the buffer is
Objectread-only will be used to initialize the byte array.
sourceis 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.
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.
b = bytes () print (b) c = bytes (None ) print (c)
b`` TypeError: cannot convert `NoneType` object to bytes pre >
With original string
Any string provided without an encoding will raise a
Similarly, trying to modify the
bytesobject 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  = 10 except TypeErro r: print (` byte objects are immutable! ` )
ExitWe 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
IRMA is a research-based professional organization dedicated to advancing the concepts and practices of information resource management in modern organizations. The primary purpose of IRMA is to promo...
Become a master in Python by learning coding best practices and advanced programming concepts in Python 3.7. This book is written for Python developers who wish to go further in mastering Python. A...
Python: - The Bible - 3 Manuscripts in 1 book:
ig Data applications are growing very rapidly around the globe. This new approach to decision making takes into account data gathered from multiple sources. Here my goal is to show how these diverse s...