Is Python string immutable?

Martin Franklin mfranklin1 at gatwick.westerngeco.slb.com
Fri Dec 2 04:24:12 EST 2005


Chris Mellon wrote:
> On 11/30/05, could ildg <could.net at gmail.com> wrote:
> 
>>In java and C# String is immutable, str=str+"some more" will return a new
>>string and leave some gargabe.
>>so in java and C# if there are some frequent string operation,
>>StringBuilder/StringBuffer is recommanded.
>>
>>Will string operation in python also leave some garbage? I implemented a
>>net-spider in python which includes many html string procession. After it
>>running for sometime, the python exe eats up over 300M memory. Is this
>>because the string garbages?
>>
>>If String in python is immutable, what class should I use to avoid too much
>>garbages when processing strings frequently?
> 
> 
> Python strings are immutable. The StringIO class provides a buffer
> that you can manipulate like a file, and then convert to a string and
> is probably most suitable for your purposes.
> 

another recipe is to build up a list with .append then when you need to
convert to a string with  "".join(alist)








More information about the Python-list mailing list