String Methods Don't Change ID?

Brett g Porter BgPorter at NOartlogicSPAM.com
Mon Jan 27 16:29:25 EST 2003


"Kamilche" <klachemin at home.com> wrote in message
news:889cbba0.0301271313.7da84213 at posting.google.com...
> I am reading a book on Python, 'Core Python Programming.' They have
> hammered home how when you change a string, or reassign it, the ID
> changes.
>
> But now, I'm looking the following example, which modifies a string,
> but the ID doesn't change. Can anyone explain why the built-in string
> methods don't modify the ID?
>
> >>> quest = 'what is your favorite color?'
> >>> id(quest)
> 10988760
> >>> quest.capitalize()
> 'What is your favorite color?'
> >>> id(quest)
> 10988760
> >>> quest.center(40)
> '      what is your favorite color?      '
> >>> id(quest)
> 10988760
> >>>

Strings are immutable (keep repeating that...)

calling ``quest.capitalize()`` returns a new string object that is
capitalized, it does not capitalize the original string:


>>> q = "i am immutable"
>>> q2 = q.capitalize()
>>> print q
i am immutable
>>> print q2
I am immutable
>>> id(q)
8092056
>>> id(q2)
8096344







More information about the Python-list mailing list