Python Program to Find and Print Address of Variable Last Updated : 05 Sep, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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() functionUsing addressof() functionUsing hex() functionMethod 1: Find and Print Address of Variable using id()We can get an address using id() function, id() function gives the address of the particular object.Syntax: id(object)where, object is the data variables.Here we are going to find the address of the list, variable, tuple and dictionary. Python # get id of list a = [1, 2, 3, 4, 5] print(id(a)) # get id of a variable a = 12 print(id(a)) # get id of tuple a = (1, 2, 3, 4, 5) print(id(a)) # get id of a dictionary a = {'a' : 1, 'b' : 2} print(id(a)) Output:14023486653475294264748411744140234904267376140234866093264Method 2: Find and Print Address of Variable using addressof()we can also get memory addresses using these functions, ctypes is a foreign function library for Python. It provides C-compatible data types and allows calling functions in DLLs or shared libraries.Syntax:addressof(c_int(object))where object is the data variablesExample: Python program to get the memory address of a variable. Python # import addressof, # c_int modules from ctypes module from ctypes import c_int, addressof # get memory address of variable a = 44 print(addressof(c_int(a))) Output:140234866278064Method 3: 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 addressid is used to get the memory of the objectobject is the dataExample: Python program to get the memory address in hexadecimal representation. Python # 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:0x7fba9b0ae8c00x5572da858b600x7fba9f3c4a100x7fba9b05b8c0 Comment More infoAdvertise with us Next Article Python | Accessing variable value from code scope S sravankumar_171fa07058 Follow Improve Article Tags : Python Python Programs python-basics Practice Tags : python Similar Reads Python Program to Find ASCII Value of a Character Given a character, we need to find the ASCII value of that character using Python. ASCII (American Standard Code for Information Interchange) is a character encoding standard that employs numeric codes to denote text characters. Every character has its own ASCII value assigned from 0-127. Examples: 2 min read Python Program to Print Hello World When we are just starting out with Python, one of the first programs we'll learn is the classic "Hello, World!" program. It's a simple program that displays the message "Hello, World!" on the screen. Hereâs the "Hello World" program:Pythonprint("Hello, World!") OutputHello, World! How does this work 2 min read Insert a Variable into a String - Python The goal here is to insert a variable into a string in Python. For example, if we have a variable containing the word "Hello" and another containing "World", we want to combine them into a single string like "Hello World". Let's explore the different methods to insert variables into strings effectiv 2 min read Python program to create dynamically named variables from user input Given a string input, our task is to write a Python program to create a variable from that input (as a variable name) and assign it to some value. Below are the methods to create dynamically named variables from user input. Using globals() method to create dynamically named variables Here we are usi 2 min read Python | Accessing variable value from code scope Sometimes, we just need to access a variable other than the usual way of accessing by it's name. There are many method by which a variable can be accessed from the code scope. These are by default dictionaries that are created and which keep the variable values as dictionary key-value pair. Let's ta 3 min read Finding IP Address using Python An IP(Internet Protocol) address is an identifier assigned to each computer and other device(e.g., router, mobile, etc.) connected to a TCP/IP network that is used to locate and identify the node in communication with other nodes on the network. IP addresses are usually written and displayed in huma 2 min read Like