Clarification needed regarding immutability of strings in Python [closed]
2024/11/7 13:55:28
Strings are immutable in Python, but in below example, a new id is generated once I start concatenating letters to initial string, this id remains constant until I assign a new string to the same name reversedString. As per my understanding of immutability, in each concatenation new id must be assigned as strings are immutable, not like lists. Please clarify the same.
In CPython, ids relate to where in memory the string is stored. It does not indicate anything about mutability. How memory is used exactly is an internal implementation detail. You could of course do a deep dive into the internals of CPython's memory management to disentangle that in-depth, but that's not really useful. (Im)mutability can simply be demonstrated:
>>> foo = 'bar'
>>> baz = foo
>>> id(foo), id(baz)
(4402654512, 4402654512)
>>> foo += 'quux'
>>> print(foo, baz)
barquux bar
>>> foo[1] = 'o'
Traceback (most recent call last):File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
Altering foo using += does not mutate baz, even though they used to refer to the same object. Altering a character directly though subscription (foo[1] = ...) does not work and raises an error. This demonstrates string immutability. How memory is allocated for this during execution is not really relevant.
Maximum Value of the LootProblem Introduction:
A thief finds much more loot than his bag can fit. Help him to find the most valuable combination of items assuming that any fraction of a loot item can b…
This question already has an answer here:Making a countdown timer with Python and Tkinter?(1 answer)Closed 8 years ago.Im having problems with a countdown clock that I was making in Python for a Raspb…
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, argum…
Hello my assignment is :Create a system that allows the user to enter their name, title, surname, Dob, email and phone number. Once details are submitted, they should be written to a file. Surnames tha…
I am a beginner and I am stuck on this problem, "Write a python code that uses a while loop to print even numbers from 2 through 100. Hint ConsecutiveEven differ by 2."Here is what I came up …
I am new and learning python 3.6 and Ive almost completed my first code project. After doing an exhaustive search to resolve my problem I have not been able to find the answer to what I am sure is a si…
Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 5…
I have two lists:a - dictionary which contains keywords such as ["impeccable", "obvious", "fantastic", "evident"] as elements of the listb - sentences which cont…
Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 2…