String Splice Assignment

Alex Martelli aleaxit at yahoo.com
Tue Jul 17 04:53:07 EDT 2001


"Erno Kuusela" <erno-news at erno.iki.fi> wrote in message
news:kuitgsgxre.fsf at lasipalatsi.fi...
> In article <4e3a8319.0107161357.690b5120 at posting.google.com>,
> gervaserules at yahoo.com (eAndroid) writes:
>
> | Why don't strings support splice assignment, and what are some
> | alternatives?
>
> as others have noted, strings are immutable. probably because
> the python "everything is a reference" semantics would make
> accidentally change someone else's string a frequent bug, and
> for efficiency reasons.
>
> you can use the array module, or a list, if you need a mutable
> sequence of characters. lists are a bit less hassle, use array if you
> need to worry about memory usage.

Easiest, IMHO, is to use the MutableString class from the
standard UserString module:

D:\>python
Python 2.1.1c1 (#19, Jul 13 2001, 00:25:06) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
Alternative ReadLine 1.4 -- Copyright 2001, Chris Gonnerman
>>> import UserString
>>> x=UserString.MutableString("initialvalue")
>>> print x
initialvalue
>>> x[7:7]=' '
>>> print x
initial value
>>>


Alex






More information about the Python-list mailing list