First a pointed to the string "Dog". Then you changed the variable a to point at a new string "Dog eats treats". You didn't actually mutate the string "Dog". Strings are immutable, variables can point at whatever they want.
First a pointed to the string "Dog". Then you changed the variable a to point at a new string "Dog eats treats". You didn't actually mutate the string "Dog". Strings are immutable, variables can point at whatever they want.
The string objects themselves are immutable.
The variable, a, which points to the string, is mutable.
Consider:
a = "Foo"
# a now points to "Foo"
b = a
# b points to the same "Foo" that a points to
a = a + a
# a points to the new string "FooFoo", but b still points to the old "Foo"
print a
print b
# Outputs:
# FooFoo
# Foo
# Observe that b hasn't changed, even though a has.
Videos
Can I modify a string in Python?
Why are Python strings immutable?
What’s the difference between modifying a string and reassigning a variable?
I think it could be very useful to have strings mutable. We could to this:
However, I guess there is a reason why strings are immutable. I wonder why?
str = 'apple' str[0] = 'A'
I'm using Treehouse for Python basics and the tutor mentions this in the context of reassigning a value to a string variable. But if the value of the string variable can be reassigned, what does it mean to say it's immutable? Is there something I'm just not getting? Thanks.
In Python mutable sequence type is bytearray see this link
This will allow you to efficiently change characters in a string. Although you can't change the string length.
>>> import ctypes
>>> a = 'abcdefghijklmn'
>>> mutable = ctypes.create_string_buffer(a)
>>> mutable[5:10] = ''.join( reversed(list(mutable[5:10].upper())) )
>>> a = mutable.value
>>> print `a, type(a)`
('abcdeJIHGFklmn', <type 'str'>)
Why they made strings like this? What was the issue not creating them mutable like other iterables ie lists? Is it implemented intentionally in this way for some reason, or they made it like this due to some implementation issue ( it become compulsion on them to do it, or optimization issue )?
Strings don't have any methods on them that allow a change to the value of the string. In other words: strings are immutable.
When you call str.replace a new string is built and returned.
>>> s1 = 'Rohit'
>>> s2 = s1.replace('R', 'M')
>>>
>>> s1
'Rohit'
>>> s2
'Mohit'
As you can see s1 still has its original value.
Strings are known as Immutable in Python (and other languages) because once the initial string is created, none of the function/methods that act on it change it directly, they simply return new strings.
So, in your example
str1='Rohit'
str1.replace('R','M')
After the .replace call, you should try evaluating str1 in your REPL. It might surprise you to see that str1 still holds the value 'Rohit'. The .replace returns a new string in which all instances of the given character are replaced with the specified character. Your REPL will print that returned string, but after that it is lost since you didn't assign it to anything.
Your second example is much the same, but with a string constant instead of a variable like str1.