70
  1. What are the design reasons of making Python strings immutable? How does it make programming easier?
  2. I'm used to mutable strings, like the ones in C. How am I supposed to program without mutable strings? Are there any best practices?
3
  • 2
    There is a mutable string type, it is called bytearray Dec 30, 2011 at 18:01
  • 7
    @JanneKarila: The semantics, however, are wildly different. It doesn't, for example, handle Unicode characters, just bytes. It's not a proper string -- with string methods -- it's an adjustable array of bytes.
    – S.Lott
    Dec 30, 2011 at 21:24
  • @JanneKarila but is it a C-string ? Oct 15, 2020 at 6:29

7 Answers 7

62

When you receive a string, you'll be sure that it stays the same. Suppose that you'd construct a Foo as below with a string argument, and would then modify the string; then the Foo's name would suddenly change:

class Foo(object):
    def __init__(self, name):
        self.name = name

name = "Hello"
foo = Foo(name)
name[0] = "J"

With mutable strings, you'd have to make copies all the time to prevent bad things from happening.

It also allows the convenience that a single character is no different from a string of length one, so all string operators apply to characters as well.

And lastly, if strings weren't immutable, you couldn't reliably use them as keys in a dict, since their hash value might suddenly change.

As for programming with immutable strings, just get used to treating them the same way you treat numbers: as values, not as objects. Changing the first letter of name would be

name = "J" + name[1:]
4
  • 4
    Sooooooo I know this is a bit late, but could you explain what you mean by mutable strings not being reliable in dicts? List are mutable, but can be put in dicts, what would make mutable strings 'unreliable'?
    – wnnmaw
    Dec 10, 2013 at 4:17
  • 6
    @wnnmaw What larsmans means is that you wouldn't be able to reliably use strings as keys to a dict, since modifying a string would change its hash, which would change the bucket the string would go to. You can see this with lists yourself by trying to use a list as a key to a dictionary (a = {}; a[[1,2]] = 1 throws a TypeError).
    – Tim Dumol
    Dec 23, 2013 at 17:24
  • But its easy to change strings: str = list(str); str[0] = "J"; str = ''.join(str) Jul 4, 2018 at 13:55
  • 4
    I don't get it. What bad consequences could happen if you replace the first letter of name? Isn't it same if I assign a totally new value to name in this case? Oct 15, 2018 at 13:20
17

Immutable strings greatly simplify memory allocation when compared with C strings: you don't guess at a length and over-allocate hoping you over-allocated enough.

They're more secure: you can never have a buffer overrun the way you can in C.

There is only one mutable string use case.

  • replacing a substring or a single character

All other string use cases (concatenation, searching, etc., etc.) the mutability does not matter. In all other cases, mutability does not matter.

If you want to replace a character or a substring in Python, you simply create a new string

x = x[:place] + replacement + x[place+1:]

That's the only code that novel or distinctive.


For reasons I fail to understand, it appears important to add the following.

"There are other ways to avoid a string buffer overflow than immutable strings."

For the purposes of this question (about Python, specifically) immutable strings have a pleasant consequence of no buffer overflows. For other languages, other principles, rules and nuances apply.

24
  • 5
    Ruby (as one example) has mutable strings, and it has automatic memory management and can't have buffer overflows. As for replacing substrings being the only use case, what about (in-place-)appending? -1
    – user395760
    Dec 30, 2011 at 13:56
  • 1
    You made (before the edit 31 minutes ago) a far more general (and, due to that generality, wrong) statement, to which I provided a counter-example. The part of the question you addressed in that part of your answer ("why make strings immutable?") didn't speak of C. And in-place appending is different from concatenating two immutable strings and putting the result back into a variable - the difference is plainly visible to all code that accesses the (old) string (instead of going through the updated variable).
    – user395760
    Dec 30, 2011 at 17:39
  • 2
    I am talking about Python, but to contrast mutable with immutable strings (part of the question and all answers, mind you), I provided an example. This example couldn't have been in Python as it has immutable strings only. In particular, in-place appending (not the contracted form of s = s + ... but the operation that modifies the string object) does not exist in Python. That's the point I'm trying to make. Your answer is incomplete regarding operations possible with mutable strings but not possible with immutable strings.
    – user395760
    Jan 1, 2012 at 15:25
  • 1
    I'd continue my argument from the (now confirmed) axioms, but it's getting ridiculous. @DavidHeffernan's comment indicates it has been stated frequently and clearly enough that one person got it. My urge to nitpick has been satisfied.
    – user395760
    Jan 5, 2012 at 12:14
  • 1
    @delnan pointed out that your original answer could be interpreted as implying that immutability was necessary to guarantee that you can never have buffer overruns. I'm sure you didn't mean that. A simple edit would have solved the issue and likely led to upvotes. If you cannot see the sarcasm in some of your comments then I'm not going to stoop to pointing it out. In my view, comments that are as heavily sarcastic as yours detract from what you bring to the site. Jan 5, 2012 at 13:01
12
  1. Immutable objects are automatically threadsafe.
  2. You will find that using Python strings is trivially easy in comparison to the extreme pain associated with strings in C.
2
  • The painfulness of C strings has reasons that have little to do with being mutable. That merely exacerbates the problem a bit. Nov 13, 2021 at 15:52
  • @leftaround I agree, but I didn't say that painfulness of C strings has anything to do with them being mutable Nov 13, 2021 at 22:38
8

Immutable strings can be keys in dictionaries and similar data structures, without the need to copy the strings. It is easier to make a mutable wrapper around an immutable string than the other way around.

3

Most languages have immutable strings. This includes Java, Python, and C#. Usually when concatenating strings, the language allocates an entirely new string and copies the content of the two strings into the new string.

Immutability does tend to make programming easier. Especially when dealing with a multi-threaded environment.

0
2

Immutable strings makes programming much easier, which is why C# and Java use them too.

Had strings been mutable, you would not be able to trust any externally-provided string, since a malicious caller could change it underneath you.
It would also make multi-threading much more difficult.

5
  • 1
    Can you give anexample? Oct 15, 2018 at 13:22
  • An example of what?
    – SLaks
    Oct 15, 2018 at 14:52
  • 1
    "you would not be able to trust any externally-provided string" for this. Thanks Oct 15, 2018 at 18:38
  • 1
    You write a function that accepts a string, but your caller mutates the string while your function is running.
    – SLaks
    Oct 15, 2018 at 19:13
  • Isn't that good sometimes? I will have a data holder which has the latest changes in it.
    – pankaj
    Jan 16, 2021 at 9:02
0

Immutable strings are much more dangerous than mutable strings in certain contexts. A best practice list should include never temporarily storing passwords or keys as strings in python.

While it is great that immutable strings are safer in string operations and offer other consistency advantages, storing (even temporarily) a password or goodness forbid a key: This puts that value in memory for the life of that program. This may end up inside a core dump file (that is improperly permissioned). If a virtual machine running the program is paused, v-motioned to a different physical machine... this allows the data in memory in this program to leak to people outside the group of users and administrators of the solution.

Due to our massively convenient/complex virtualization stack we've built, you need to do your best to avoid accepting passwords or storing keys in memory. There is precious little information on how to do this, but it's best to follow solutions that use OS native mechanisms designed to perform these operations properly including openssl, ssh, keyring, etc.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.