w3resource

Python: Convert an array to an array of machine values and return the bytes representation

Python: Array Exercise-8 with Solution

Write a Python program to convert an array to an array of machine values and return the bytes representation.

Sample Solution:

Python Code :

from array import *
print("Bytes to String: ")
x = array('b', [119, 51, 114, 101,  115, 111, 117, 114, 99, 101])
s = x.tobytes()
print(s)

Sample Output:

Bytes to String:                                                       
b'w3resource'

Python Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Python program to append items from inerrable to the end of the array.
Next: Write a Python program to append items from a specified list.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Python: Membership Testing in a Collection

>>> a = ('one', 'two', 'three', 'four', 'five')
>>> if 'one' in a:
...     print('The tuple contains one.')
... 
The tuple contains one.
>>> b = {0: 'zero', 1: 'one', 2: 'two', 3: 'three'}
>>> if 2 in b.keys():
...     print('The dict has the key of 2.')
... 
The dict has the key of 2.