Slice assignment for strings?

Discussion in 'Python' started by Jens Lippmann, May 25, 2004.

  1. 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:eek:fs+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
    Jens Lippmann, May 25, 2004
    #1
    1. Advertising

  2. Jens Lippmann

    John Roth Guest

    "Jens Lippmann" <> wrote in message
    news:c8vre2$ruk$04$-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:eek:fs+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
    >
    >
    John Roth, May 25, 2004
    #2
    1. Advertising

  3. Jens Lippmann wrote:
    > 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 in python; it means you cannot change them
    in-place. You can still change the value of variables and do things like:
    a = "hello"
    a = "a" + a[2:] # producing allo

    Strings are immutable, but there's (mutable) containers that might do a
    better job for what you want to do. Module array could be a possible
    solution (and I'm sure there's others).

    Regards,
    Nicolas
    Nicolas Fleury, May 25, 2004
    #3

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


    ok, thanks!
    Jens Lippmann, May 25, 2004
    #4
  5. Jens Lippmann

    Paul Rubin Guest

    "Jens Lippmann" <> writes:
    > 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?


    You can use the array module for what you're doing. See the docs.
    Paul Rubin, May 25, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rich Krauter

    Assignment to slice

    Rich Krauter, Jan 21, 2004, in forum: Python
    Replies:
    0
    Views:
    340
    Rich Krauter
    Jan 21, 2004
  2. James Henderson

    Re: Assignment to slice

    James Henderson, Jan 21, 2004, in forum: Python
    Replies:
    2
    Views:
    268
    James Henderson
    Jan 22, 2004
  3. Rich Krauter

    RE: Assignment to slice

    Rich Krauter, Jan 21, 2004, in forum: Python
    Replies:
    6
    Views:
    329
    sambo
    Jan 23, 2004
  4. Andrew
    Replies:
    2
    Views:
    222
    Stephen Horne
    Sep 21, 2008
  5. Daku
    Replies:
    0
    Views:
    693
Loading...

Share This Page