w3resource

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

Python Basic: Exercise-117 with Solution

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.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://198.211.115.131/python-exercises/python-basic-exercise-117.php