Python | Same and Different value space
In Python, we have same pool for similar values, even if they share different variables. This has many advantages, that it saves a lot of memory arranging different spaces for similar values. This is provided for small integers. But there are ways in which we can have same and different pool values. Lets discuss certain cases of the same. Case #1 : Same pool ( Using + operator ) In this, when we create a String using “+” operator, we create a space that will point to similar space in memory.
The original string is : gfg Are values pointing to same pool ? : True
Case #2 : Different Pool ( Using join() + ord() ) This is way in which we need to initialize string pointing to different value space in memory. This can be achieved using join() for joining ASCII values extracted using ord().
The original string is : abc Are values pointing to same pool ? : False
The Time and Space Complexity for all the methods are the same:
Time Complexity: O(n)
Space Complexity: O(n)