0

I have declared a string "y as hello than tried to change character "h" by "m" using replace method in python and i checked for the type(y): its showing string but when I googled, its showing strings are immutable... please explain

>>> y="hello"

>>> y=y.replace("h","m")

>>> y


'mello'
>>> type(y)

<class 'str'>
Carcigenicate
  • 43,494
  • 9
  • 68
  • 117
  • 4
    You didn't mutate a string. You reassigned a variable with an entirely new string. – jasonharper Mar 12 '19 at 21:48
  • Try `x = y = 'hello'` and print `x` afterwards and you will see that it still has the value `hello`. If you do the same with a `list` (a mutable value) then you will see any change reflected in `x`, e.g. `x = y = [0, 1, 2]; y[0] = 5; x` -> `[5, 1, 2]` – AChampion Mar 12 '19 at 21:49
  • @AChampion with an [important caveat](https://stackoverflow.com/a/24245514/4799172) about re-used memory locations – roganjosh Mar 12 '19 at 21:53
  • 1
    @roganjosh I decided to avoid this with a different approach than `id()` – AChampion Mar 12 '19 at 21:55
  • @AChampion yeah, I just realised you edited after I typed so my comment isn't relevant to your new comment, but I think it's probably worth keeping the link – roganjosh Mar 12 '19 at 21:56

3 Answers3

1

You didn't mutate the String, you changed what String y pointed to.

y originally pointed to "hello", then you ran the line y=y.replace("h","m") and caused y to instead point to the String "mello". The original String "hello" was never mutated, since Strings are indeed immutable.

Carcigenicate
  • 43,494
  • 9
  • 68
  • 117
0

Yes, strings are immutable. When you run the line y=y.replace("h","m") you are creating a new string, not changing the first one. The first string can't actually be changed.

Rob Kwasowski
  • 2,690
  • 3
  • 13
  • 32
-1

String in Python are immutable (cannot change to allow for optimization of using one dictionary for all strings).

For example:

>>> y="hello"

>>> y.replace("h","m")
'mello'

>>> y
'hello'

When we want to change a string, we can either use bytearray or (often better) - just create a new string. Consider for example the method 'replace'. It did not change the string in the example - but we could assign it's return value.

>>> y1 = y.replace("h","m")
>>> y1
'mello'

Or even use the same variable y, in which case it will create a new string and overwrite the previous value of y.

>>> y = y.replace('h','m')
>>> y
'mello'

Which is just like putting a new value alltogether:

>>> y = 'a new value'
>>> y
a new value
Ofer Rahat
  • 790
  • 1
  • 9
  • 15
  • Strings are _not_ mutable. I don't know what you're trying to say here. You haven't mutated a string in terms of mutable/immutable, you are always creating a new string. – roganjosh Mar 12 '19 at 22:07
  • 1
    Nowhere in this answer have you mutated a String. Please be careful when posting things like this. We don't need to confuse the OP more than they already are. – Carcigenicate Mar 12 '19 at 22:36
  • 1
    Wait, is the first paragraph supposed to say "String[s] in Python are **immutable**"? You seem to be saying that they're immutable in the rest of the answer, but you're contradicting the first paragraph. – Carcigenicate Mar 13 '19 at 01:45
  • yes - sorry, fixed now – Ofer Rahat Mar 13 '19 at 18:16