Aren't strings immutable?

ohh, NO, at first I was thinking to check i and then add i + 1 to the new_string so -1 will prevent me from going out of range and then it comes to my mind that using % will be easier and better so I just forgot to remove it.
Thanks for the feedback
Now I am wondering why it didn’t raise an error?
is that mean I didn’t check the last character ?

That’s what it looks like.

1 Like

This question is stumping me too. And with all due respect, the explanation doesn’t explain it for me – i don’t “get” it. I have an immutable string but as I iterate I change it. If the variable name is the same, in what sense is that string immutable? I set a variable to a string value, then change the value, then change it again. Semantically, what does “immutable” mean if I can change the value associated to the variable name? Thanks for helping me to understand.

A variable can take new assignments. The object it is assigned may be immutable, but that has no bearing on the variable.

Thank you! I think i get it now.

So the variable is a pointer to a memory address that holds a particular string, which can’t change. When we change the value of a variable, we are pointing it to a new memory address holding a different string.

How does that differ from working with a mutable list? Presumably, when we update a list, instead of changing the pointer to a different block of memory, we continue to point to the original memory block, but what it holds is being changed.

But from the standpoint of a coder, does it make any difference? When I append a value to a list, or when I set the value of variable str_a to str_a + str_b, is there any real difference? Or from a coder’s standpoint is it just a matter of what methods are available?

thank you.

2 Likes

When we mutate an element in a list (or an item in a dict), it doesn’t change the object the variable points to, only a mutable part of it. Only when we replace the assignment with another object is the reference changed.

a = "strings are immutable"

b = ['list elements are mutable', 'element', 'element', 'etc.']

a points to a single value. We cannot change that value, but we can change what a points to.

a = a.upper()

The old value is now gone.

 b[0] = b[0].upper()

The first element is replaced with a new value, but b still points to the same collection (data structure reference) of values.

2 Likes

I’m going to repeat one quetion that the person above asked because I want to make sure I get it right; But from the standpoint of a coder, does it make any difference?

Sorry, you got me. We’re talking mutable objects here. What difference should we be looking at?

[quote=“cristian_carranza, post:36, topic:459978”]
from the standpoint of a coder, does it make any difference?

I know that when we change a string variable, we’re not changing the string but what the variable points at.
I’m going to copy and paste what the person I was mentioning above wrote:

So the variable is a pointer to a memory address that holds a particular string, which can’t change. When we change the value of a variable, we are pointing it to a new memory address holding a different string.

How does that differ from working with a mutable list? Presumably, when we update a list, instead of changing the pointer to a different block of memory, we continue to point to the original memory block, but what it holds is being changed.

But from the standpoint of a coder, does it make any difference? When I append a value to a list, or when I set the value of variable str_a to str_a + str_b, is there any real difference? Or from a coder’s standpoint is it just a matter of what methods are available?

You already respond to that comment, but I want to get an explanation for this specific question:
But from the standpoint of a coder, does it make any difference? When I append a value to a list, or when I set the value of variable str_a to str_a + str_b, is there any real difference? Or from a coder’s standpoint is it just a matter of what methods are available?

Fortunately for us code is not a magic eight ball. We do pull the strings.

A reference to an object is pretty plain. But what about when that object contains multiple addressable object references?

>>> a = 42
>>> id(a)
140732528053184
>>> a = 6 * 7
>>> id(a)
140732528053184
>>> a = None
>>> id(a)
140732527822976
>>> b = []
>>> id(b)
3136412316928
>>> b.extend([6, 7])
>>> a = 6
>>> id(a)
140732528052032
>>> id(b[0])
140732528052032
>>> 

I know a code can be written in so many ways but i want to know efficient and best way of code writing.
Actually, i can’t decide which code is efficient. can you please tell me how to decide which code is efficient?

At the beginner stage we need not concern ourselves with efficiency. That comes up when we actually know how to code and can understand what the code is doing and why. For now just focus on learning the concepts and syntax. Commit the keywords and constructs to memory and practice extensively with each new lesson. Only when you reach the advanced stage will it become important to consider complexity and resource usage. That is a long ways down the road.

2 Likes