Why are strings immutable?

Ben Last ben at benlast.com
Sat Aug 21 11:47:18 CEST 2004

> From: Brent W. Hughes
> I kind of hate to have to convert a string into a list, manipulate it, and
> then convert it back into a string.  Why not make strings mutable?
There are a whole host of good reasons, mostly to do with the way data types
are seen from a Python point of view.  However, if you're converting strings
to lists and back, you're probably using recipes from another language (such
as C, perhaps?) where there are more Pythonesque equivalents.  Take a look
at all the string methods for starters; they operate on the contents of a
string and return the modified result.  It's common to stack them up, as in:


It's sometimes useful to remember that you can iterate over a string
character by characters (technically, in substrings one character long):

>>>for x in "abc":
...  print x

What is it you're trying to do?


More information about the Python-list mailing list