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.
sample = "hello"
print(id(sample)) # 1635882773744
sample += "A"
print(id(sample)) # 1635885488752
sample += "D2"
print(id(sample)) # 1635885488752
sample += "EWWW"
print(id(sample)) # 1635885488752
sample = "R"
print(id(sample)) # 1635795667504
Output:
1635882773744
1635885488752
1635885488752
1635885488752
1635795667504