Mutable Strings?

Scherer, Bill Bill.Scherer at VerizonWireless.com
Tue Jun 26 11:25:19 EDT 2001


On Tue, 26 Jun 2001, Eric Merritt wrote:

> Guys,
>
>   I am somewhat new to python and I have a bit of a
> question  (as a side note, at work, I am a java
> programmer mostly). In python, as in java, strings are
> immutable objects (types in python I know). In any
> case, Java has provided a StringBuffer class that is
> basically a mutable string. Allowing you to append,
> insert etc without creating a new object.  This saves
> quite a bit of object creation when any string
> processing is required.

Try using a list, and then join it when you need the string:

Python 2.1 (#1, Apr 24 2001, 13:32:11)
[GCC egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)] on linux2
Type "copyright", "credits" or "license" for more information.
>>> a = ['a', 'b', 'c']
>>> ' '.join(a)
'a b c'
>>> a.append('d')
>>> ' '.join(a)
'a b c d'
>>> a.insert(3, 'wow!')
>>> ' '.join(a)
'a b c wow! d'



>
> I have been looking for similar functionality in
> python and have yet to find it. I tried searching the
> archives but any search for terms relating to strings
> returns an enormous amount of hits. In any case, are
> any of you aware of a anything in python that provides
> this type of functionality? I am willing to write my
> own I just don't want to reinvent the wheel.
>
>
> __________________________________________________
> Do You Yahoo!?
> Get personalized email addresses from Yahoo! Mail
> http://personal.mail.yahoo.com/
>
>

William K. Scherer
Sr. Member of Applications Staff - Verizon Wireless
Bill.Scherer_at_VerizonWireless.com





More information about the Python-list mailing list