Why are strings immutable?

Brent W. Hughes brent.hughes at comcast.net
Mon Aug 23 21:58:38 CEST 2004


Let me give two examples of where I think it would be nice to be able to
change strings in place:


First example:

I want to add about 20,000 words to the end of a string, something like
this:

Str = [ ]
for i in range(20000):
 Word = DoSomeProcessing()
 Str += Word

I'd actually like to say Str.extend(Word).  As it is, I'm thinking of
something like this:

List = [ ]
for i in range(20000):
 Word = DoSomeProcessing()
 List.extend(list(Word))
Str = ''.join(List)


Second example:

I would like to reverse a string containing about 120,000 characters.  I'd
like to do it in place, but I'm planning to do something like this:

List = list(Str)
List.reverse()
Str = ''.join(List)





More information about the Python-list mailing list