w3resource

Python Data Structure: Read a string and interpreting the string as an array of machine values

Python Data Structure: Exercise-12 with Solution

Write a Python program to read a string and interpreting the string as an array of machine values.

Sample Solution:

Python Code:

from array import array
import binascii
array1 = array('i', [7, 8, 9, 10])
print('array1:', array1)
as_bytes = array1.tobytes()
print('Bytes:', binascii.hexlify(as_bytes))
array2 = array('i')
array2.frombytes(as_bytes)
print('array2:', array2)

Sample Output:

array1: array('i', [7, 8, 9, 10])                                                                             
Bytes: b'0700000008000000090000000a000000'                                                                    
array2: array('i', [7, 8, 9, 10])   

Flowchart:

Flowchart: Read a string and interpreting the string as an array of machine values

Python Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Python program to convert an array to an array of machine values and return the bytes representation.
Next: Write a Python program to push three items into the heap and print the items from the heap.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.