In this article, we will learn about python bytes() method with examples. We will learn about syntax, parameter, return value etc. of bytes() method.
Getting Started
bytes() method converts an object into immutable bytes object with given size and data.
Difference between bytes() and bytearray() is that bytes() method returns an immutable object while bytearray() returns mutable object.
Syntax of bytes() Method
Syntax of bytes() Method in python is –
bytes(source, encoding, errors)
Parameters of bytes() Method
bytes() method have 3 parameters –
- source: Source which is to be converted.
- encoding: Encoding to be used when source is string.
- errors: Action to be taken when error occurs while converting using encoding.
Return Value of bytes() Method
bytes() method returns an immutable bytes object with given size and data. Return value consists of unicode 0-256 characters as per source type.
If source is –
- Integer: bytes() returns array of given size, initialised to null.
- String: bytes() returns encoded string as per value given in encoding. If error occurs, performs action provided in errors argument.
- Iterable Object: Returns array of iterable object size and elements present in the iterable object. Iterable Object can be list, set etc.
- Empty : Returns an empty and immutable array of size 0.
Examples of bytes() Method
Now, we will see different examples of bytes() method in python.
-
Case 1: Creates array of bytes From Integer Source
If source is an integer value, bytes() method creates an array with that size. For example,
size = 8 newArray = bytes(size) print(newArray)
Output:
b'\x00\x00\x00\x00\x00\x00\x00\x00'
Here,
An array of bytes of size 8 is created. -
Case 2: Creates array of bytes From Iterable List
If source is iterable list, bytes() method create an array of bytes with same size and initializes it elements of the iterable list. For example,
listA = [4, 3, 2, 5] newArray = bytes(listA) print(newArray)
Output:
b'\x04\x03\x02\x05'
Here,
byte array has size 4 and elements are similar as that of iterable list.Example 2:
Let’s take another example,
listA = [1, 6, 5, 500] newArray = bytes(listA) print(newArray)
Output:
ValueError: bytes must be in range(0, 256)
We must has all elements of iterable list in the range of 0 to 256.
Example 3:
Let’s take another example,
listA = [6, 2, 7, 8.0] newArray = bytes(listA) print(newArray)
Output:
TypeError: 'float' object cannot be interpreted as an integer
All elements of iterable list must be integer. Otherwise, a TypeError will be thrown.
-
Case 3: Creates array of bytes from Object
We can also create byte array if source is an object. For example,
# Creates bytearray from byte literal newArrayA = bytes(b"hello") # iterating the value in newArray1 for value in newArrayA: print(value) # Create a bytearray object newArrayB = bytes(b"Tutorialwing") # Counts bytes from the buffer print("Count of i is:", newArrayB.count(b"i"))
Output:
104 101 108 108 111 Count of i is: 2
-
Case 4: Creates array of bytes From Empty Source
If source is empty, byte array of Size 0 is created. For example,
newArrayB = bytes() print(newArrayB)
Output:
b''
-
Case 5: Creates array of bytes From String Source
If source is string, we must provide encoding. However, error parameter is still optional.
str = "Tutorialwing" # Creates array of bytes # using encoding utf-8 and utf-16 newArray1 = bytes(str, 'utf-16') newArray2 = bytes(str, 'utf-8') print(newArray1) print(newArray2)
Output:
b'\xff\xfeT\x00u\x00t\x00o\x00r\x00i\x00a\x00l\x00w\x00i\x00n\x00g\x00' b'Tutorialwing'
Learn more at official doc.
That’s end of our post on python bytes() Method with examples.