+ 2
Why updating original string not giving an error even though they are immutable ?
In python Solo Learn say strings are immutable means we cannot change them after defining them, then why this code works: text = "Good" text += "Morning" print(text) Didn't I just modified the string ie change the string but it still works. Why does the above code works ?
12 Réponses
+ 9
You did not modify the string. You created a new string, and assigned it to the same variable. The original string "Good" does not have any references to it any more, and will be garbage collected (cleaned from memory).
+ 10
Parveen ,
i have added 2 new lines of code to your code (you can just run it).
you can see what id variable has after creation and after the operation. the id is different, so we have 2 different objects.
https://sololearn.com/compiler-playground/cCwb4nF7h0Kn/?ref=app
+ 7
Yes, Strings are immutable in Python. Immutable means that cannot be changed. So in case-
word=["Cute"]
word[1]="c"
print(word)
#It will result in error as we are trying to change the "u" in "Cute" with "c".
But in your code . i.e.~
text="Good"
text+="Morning"
print(text)
#It will print GoodMorning because it means you are adding one more string just after the original string ends but not changing any value from inside the string.So , in your code concatenation of the strings occur.
Hope it helps!
+ 6
for strings, = and += are reassignment operations, not modification.
this is modification, and Python will throw an error if you do this.
name = "Bill"
name[0] = 'J'
although Python is smart enough not to reassign if you assign it the same value
name = "Bill"
print(id(name))
name = "Bill"
print(id(name)) #same id
name = " Bill" #extra space
print(id(name)) # different id
+ 5
Parveen ,
+= is one of the "in-place" operators. (Its dunder equivalent even has i in the name to remind us of "in-place", __iadd__). And in-place operators are supposed to try to mutate the original object in place (not create a new object) whenever possible.
https://peps.python.org/pep-0203/
However, str is considered an immutable type in the language specifications. So, for str objects, this form of assignment,
a += b
behaves the same as this form (it creates a new object).
a = a + b
The two forms behave differently for mutable objects.
https://docs.python.org/3/library/operator.html#in-place-operators
There is a rare exception. In the CPython implementation of Python, some str objects will occasionally retain the same id when += is used, but it's just a CPython optimization, and not proof that str is a mutable type. See https://austinhenley.com/blog/pythonstringsaremutable.html
+ 4
Rain ,
do you think that sololearn has done backups from this *deleted* data? so it should be possible to restore all these things.
+ 2
In Python, strings are indeed immutable, which means you cannot change them in place. However, what you are doing in the code snippet `text += "Morning"` is not actually modifying the original string in place. Instead, you are creating a new string by concatenating the original string (`"Good"`) and another string (`"Morning"`), and then reassigning that new string to the variable `text`.
So when you do `text += "Morning"`, you are not directly modifying the original string `"Good"`. Rather, you are creating a new string `"GoodMorning"` and assigning it to the variable `text`. This is why the code works and you don't get an error.
When you are changing the value of the variable `text`, you are not actually changing the original string in memory. It's important to differentiate between updating the value of a variable (which is allowed) and directly modifying an immutable object in place (which is not allowed with strings).
+ 2
Wong Hei Ming ,
OK. Found it. I got the link from Lisa , who had pasted it into a course comment.
course: Python Intermediate
module 1: Collection Types
lesson 1: Tuples
page: 16
Note that Sololearn updated Python Intermediate and tragically deleted all users' comments! So anyone who enrolls in it now or resets the old course will not see that link. I only found the page number because I had luckily kept some external notes in addition to saving the link as a browser bookmark.
+ 2
Lothar ,
I think (but am unable to confirm) that only people who took the course before the update and haven't reset it still see the old version, meaning the data still exist but are not accessible by everyone. I can't see it anymore, because I reset the course.
+ 2
Rain Lothar
I believe SL has "created" "new version" of the same course, and the "old version" is hidden from new learner.
I once asked the team about a disappearance of a chapter in Intro to SQL (reset) but the same chapter is available in Data Programming, and they reply "the version" I enrolled doesn't has that chapter.
And recently I reset Intro to JS, the oldest comments are 4 months old. What a shame the old comments are not accessible...
+ 1
Rain
I think you got the link somewhere else, not from me.
I guess you found it when researching on CPython optimization.
0
it is because you are string concatenation, it is string to string addition so it's possible, if it was a string and an integer so it was not going to be possible