Slice assignment for strings?

John Roth newsgroups at jhrothjr.com
Tue May 25 12:30:09 EDT 2004


"Jens Lippmann" <jens-nospam at t-online.de> wrote in message
news:c8vre2$ruk$04$1 at news.t-online.com...
> Hi all!
>
> I'm new to Python and just tried to assign values to a portion
> of a string, but I don't get it.
> My code is:
>         bits = '\3ff' * bmi.bmiHeader.biSizeImage
>         ofs = 0x1E2C0
>         for i in range(0, height):
>           bits[i*linesize:(i+1)*linesize] = self.shbits[ofs:ofs+width/2]
>
> which leads to
>   ...
>   TypeError: object doesn't support slice assignment
>
> 'bits' is a binary string resp. an array of byte so to say.
> Note also that the right slice is shorter than the left one.
>
> So, my questions are:
> - How do I get it right?
> - If slicing seems to be the favorite way to address substrings, why
>   is it impossible to use it on a left side of an assignment?

Strings are immutable objects, so you can't change them in
any way.

John Roth
>
> kind regards
> Jens Lippmann
>
>





More information about the Python-list mailing list