how are strings immutable in python?

Lie Lie.1296 at gmail.com
Tue Jul 8 12:53:17 CEST 2008


On Jul 7, 1:45 am, ssecorp <circularf... at gmail.com> wrote:
> >>> h = "aja baja"
# 'aja baja' is created and assigned to the name h

> >>> h += 'e'
# Is the equivalent of:
# h = h + 'e'
#
# In there, a h and 'e' is concatenated and assigned to
# a new string object which is bound to h. The
# original string object which contains
# 'aja baja' is "garbage collected"[1].

> >>> h

# printing h which contains the new string object containing 'aja
bajae'

> 'aja bajae'

[1] actually python uses reference counting first to choose which
object to delete



More information about the Python-list mailing list