w3resource

Python: Get variable unique identification number or string


Get Variable ID or String

Write a Python program to get a variable with an identification number or string.

Sample Solution:

Python Code:

# Define a variable 'x' and set its value to 100
x = 100

# Print the hexadecimal representation of the unique identifier (memory address) of 'x'
# 'id(x)' returns the memory address, and 'format(id(x), 'x')' formats it as a hexadecimal string
print(format(id(x), 'x'))

# Define a string 's' with the value 'w3resource'
s = 'w3resource'

# Print the hexadecimal representation of the unique identifier (memory address) of the string 's'
# 'id(s)' returns the memory address, and 'format(id(s), 'x')' formats it as a hexadecimal string
print(format(id(s), 'x'))

Sample Output:

7f71e94c4d50
7f71e9405df0 

Flowchart:

Flowchart: Get variable unique identification number or string

For more Practice: Solve these Related Problems:

  • Write a Python program to find the memory address of a given list and compare it with another list.
  • Write a Python program to convert an object reference to a string representation.
  • Write a Python program to check if two different variables point to the same memory location.
  • Write a Python program to create a unique identifier for objects and store them in a dictionary.

Python Code Editor:

Previous: Write a Python program to create a list by concatenating a given list which range goes from 1 to n.
Next: Write a Python program to find common items from two lists.

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.