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

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.