Find and Print Address of Variable using hex()
Here we will call the hex(address) function, to convert the memory address to hexadecimal representation.
Syntax: hex(id(object))
where,
- hex() is the memory hexadecimal representation to the address
- id is used to get the memory of the object
- object is the data
Example: Python program to get the memory address in hexadecimal representation.
Python3
# get id of list in hexadecimal representation a = [ 1 , 2 , 3 , 4 , 5 ] print ( hex ( id (a))) # get id of a variable in hexadecimal representation a = 12 print ( hex ( id (a))) # get id of tuple in hexadecimal representation a = ( 1 , 2 , 3 , 4 , 5 ) print ( hex ( id (a))) # get id of a dictionary in hexadecimal representation a = { 'a' : 1 , 'b' : 2 } print ( hex ( id (a))) |
Output:
0x7fba9b0ae8c0 0x5572da858b60 0x7fba9f3c4a10 0x7fba9b05b8c0
Python Program to Find and Print Address of Variable
In this article, we are going to see how to find and print the address of the Python variable.
It can be done in these ways:
- Using id() function
- Using addressof() function
- Using hex() function