a simple question about string object

Greg Krohn ('rot-13') 'tert at pncen.hf'.decode
Sun Mar 16 22:03:46 EST 2003


"Frank Zheng" <hust_zxq524 at 263.net> wrote in message
news:b53954$7bu$1 at mail.cn99.com...
> Hi, all:
>
> >>> s = '12345'
> >>> s[2] = 'x'
> Traceback (most recent call last):
>   File "<interactive input>", line 1, in ?
> TypeError: object doesn't support item assignment
>
> i know the string object is immutable, so if i want to do 'item
assignment',
> i should do:
> >>> new_s = s[0:1] + 'x' + s[2:]
> ( i think it's ugly)
>
> Is there other ways to do this?
> should i define a function to do this? Is there any modules existing can
do
> this?
>
>
> thanks
>
> frank
>

Is this less ugly?

>>> s = '12345'
>>> s = list(s)
>>> s[2] = 'x'
>>> s = ''.join(s)
>>> s
'12x45'







More information about the Python-list mailing list