Slice assignment for strings?

Jens Lippmann jens-nospam at t-online.de
Tue May 25 12:17:53 EDT 2004


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?

kind regards
Jens Lippmann





More information about the Python-list mailing list