w3resource

Python: Prove that two string variables of same value point same memory location


String Memory Location Test

Write a Python program to prove that two string variables of the same value point to the same memory location.

Sample Solution:

Python Code:

# Define two string variables, str1 and str2, both containing the string "Python".
str1 = "Python"
str2 = "Python"
 
# Print the memory location (in hexadecimal) of str1 and str2 using the id() function.
print("\nMemory location of str1 =", hex(id(str1)))
print("Memory location of str2 =", hex(id(str2)))

# Print a blank line for separation.
print()

Sample Output:

Memory location of str1 = 0x7f8af3e89f10                                                                      
Memory location of str2 = 0x7f8af3e89f10

Flowchart:

Flowchart: Prove that two string variables of same value point same memory location.

Python Code Editor:

 

Previous: Write a Python program to print Unicode characters.
Next: Write a Python program to create a bytearray from a list.

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.