5

Just curious more than anything why python will allow me to update a slice of a list but not a string?

>>> s = "abc"
>>> s[1:2]
'b'
>>> s[1:3]
'bc'
>>> s[1:3] = "aa"

>>> l = [1,2,3]
>>> l[1:3]
[2, 3]
>>> l[1:3] = [9,0]
>>> l
[1, 9, 0]

Is there a good reason for this? (I am sure there is.)

Justin Ethier
  • 131,333
  • 52
  • 229
  • 284
Chris
  • 11,780
  • 13
  • 48
  • 70
  • "Is there a good reason for this?" Yes. Strings are not lists. Seriously, that's the reason. They work differently. – S.Lott Nov 19 '10 at 18:15

2 Answers2

9

Because in python, strings are immutable.

Justin Ethier
  • 131,333
  • 52
  • 229
  • 284
  • +1. This is true in many programming languages (which provide a string implementation) – Matt Ball Nov 19 '10 at 14:12
  • Great point, I totally forgot about this aspect of python strings. Sorry for wasting time. – Chris Nov 19 '10 at 14:17
  • Don't beat yourself up over it. Plus, it might help the next guy that happens to stumble across this question :) – Justin Ethier Nov 19 '10 at 14:51
  • @Justin Ethier: Since the question is a duplicate, it probably won't help anyone. For example http://stackoverflow.com/questions/1228299/change-one-character-in-a-string-in-python is essentially the same question. – S.Lott Nov 19 '10 at 18:16
  • @S.Lott that question is not the same at all. Just because a question touches on the same answer does not mean its the same question. Two different paths leading to the same answer. That is to say, I came from japan to get to america, and he came from brazil to get to america, they must have come from the same place. Right? – Chris Nov 19 '10 at 18:46
  • @Chris: "that question is not the same at all"? Please **update** this question to provide some evidence or hint that it's not the same question. It sounds like exactly the same question. Your job is to how that your question really is unique. – S.Lott Nov 19 '10 at 19:21
  • 1
    "Change on character in a string (in python)" versus "Why can I update a list slice but not a string slice?" Topic seems to distinguish them enough. And at this point I frankly do not care, the answer satisfied myself. If you think its a duplicate vote to close it. – Chris Nov 19 '10 at 19:29
  • @Chris: I like your attitude. No reason to help others. You were satisfied. The other users who land on the questions need no help. Thanks for making the site work well only for you. – S.Lott Nov 19 '10 at 19:33
  • Seriously? What does this question need to differentiate it from that question other than the topic which is quite different? I am confused as to what problem you seem to have with this question and that question and their similarities/differences? You have enough reputation to edit this question mate so feel free to edit it to your hearts desire if you feel it needs it. I personally do not think it needs anything more to distinguish itself. Take your attitude towards your statement "You were satisfied. The other users who land on the questions need no help." and edit my question, – Chris Nov 19 '10 at 19:49
  • There's really no need to argue about this: http://blog.stackoverflow.com/2010/11/dr-strangedupe-or-how-i-learned-to-stop-worrying-and-love-duplication/ – Justin Ethier Nov 19 '10 at 21:32
  • @Justin Ethier: The blog post isn't terribly helpful. @Chris. You're right. It's perfect. No one will ever be confused by the subtle differences in wording. We're all as smart as you and none of us need any extra help in seeing the differences which are so glaringly obvious to you. Because I was confused does not mean that a single other person will ever be confused. You're so right that I'm the only person who's ever going to be confused by this. Certainly, don't attempt to fix it or clarify anything. You're right about the level of perfection shown in this perfect question. – S.Lott Nov 23 '10 at 04:13
5

Python distinguishes mutable and immutable data types. Making strings immutable is a general design decision in Python. Integers are immutable, you can't change the value of 42. Strings are also considered values in Python, so you can't change "fourty-two" to something else.

This design decision allows for several optimisations. For example, if a string operation does not change the value of a string, CPython usually simply returns the original string. If strings were mutable, it would always be necessary to make a copy.

Sven Marnach
  • 574,206
  • 118
  • 941
  • 841