473,396 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Slice assignment for strings?

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
Jul 18 '05 #1
4 2619

"Jens Lippmann" <je*********@t-online.de> wrote in message
news:c8*************@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

Jul 18 '05 #2
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
Jul 18 '05 #3
Strings are immutable objects, so you can't change them in
any way.


ok, thanks!
Jul 18 '05 #4
"Jens Lippmann" <je*********@t-online.de> 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.
Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Rich Krauter | last post by:
I do not understand why python behaves the way it does in the following code: Here, Python doesn't like that I'm assigning out-of-range. OK. That's fine. >>> x = >>> x = 3 Traceback (most...
2
by: Will McGugan | last post by:
Hi, I'd like to suggest an addition to the slice syntax. How about allowing strings as arguments to slices on strings, and interpret them as the index of the substring found within the string...
40
by: Ron Adam | last post by:
After considering several alternatives and trying out a few ideas with a modified list object Bengt Richter posted, (Thank You), I think I've found a way to make slice operation (especially far end...
11
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to...
23
by: Antoon Pardon | last post by:
Now slices are objects in python, I was wondering if slice notation will be usable outside subscribtion in the future. Will it ever be possible to write things like: a = 4:9 for key, value in...
7
by: Alexandre Guimond | last post by:
Hi all, i'm trying to deepcopy a slice object but i get the following error. Does anyone know a workaround? ActivePython 2.4.3 Build 12 (ActiveState Software Inc.) based on Python 2.4.3 (#69,...
8
by: John Salerno | last post by:
Hey all. I've decided I let my Python skills (minor though they were) slip away so I started reading the new edition of Learning Python to brush up. I just read about lists again and I'm wondering...
2
by: Andrew | last post by:
please explain this behavior to a newb:
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.