Python strings are immutable? | Sololearn: Learn to code for FREE!

+8

Python strings are immutable?

I started my Programming career with python! I read that Python's strings are immutable!! But I wondered, how it is possible to use "replace()" function? Strings are immutable in Python!!!

6/3/2018 8:40:41 AM

Hemath Kumar

17 Answers

New Answer

+10

Oh, to be honest I think deep down there it iterates through the input string and concatenates char by char, replacing the spotted pattern with new one. I see no other option to build the new string making sure you got all the replacements done.

+14

replace() actually returns a *copy* of the argument string with relevant parts replaced. The original string is left intact.

+6

Strings are immutable in Python. t1="abcde" t2=t1.replace("b", "B") print(t1) #abcde print(t2) #aBcde As you can see, the replace method doesn't change the value of the string t1 itself. It just return a new string which can be used for other purposes. Of course, you can write: t1=t1.replace("b", "B") to re-assign the value of t1, but again, the original text of t1 is immutable.

+6

I made another sample to show the difference between mutable objects an immutable ones. With mutables the object id stays the same if the object is changed: t1="abcde" print("t1", t1, id(t1)) t2=t1.replace("b", "B") print("t1", t1, id(t1)) #abcde print("t2", t2, id(t2)) t1=t1.replace("b", "B") print("t1", t1, id(t1)) l1 = [1, 2] print("l1", l1, id(l1)) l1.append(3) print("l1", l1, id(l1))

+2

I am not sure how this method work behind the screen, but in my opinion, it may work like this or something alike: class str: def __init__(self, value): self.value=value def replace(self, old, new): new_str="" for i in self.value: if i==old: new_str += new else: new_str += i return str(new_str) It doesn't change the copy, but create a new string by using the original string as a reference.

+1

Refer to the concept of constant pool... 👈Please vote up if it really helped you..

+1

I made anoter sample to show the differenmce between mtable objects a immutable oes. With mutables the object id stays the same if the object is changed: t1="abcde" print("t1", t1, id(t1)) t2=t1.replace("b", "B") print("t1", t1, id(t1)) #abcde print("t2", t2, id(t2)) t1=t1.replace("b", "B") print("t1", t1, id(t1) l1 = [1, 2] print("l1", l1, id(l1)) l1.append(3) print("l1", l1, id(l1))

+1

In Python, strings are made immutable so that programmers cannot alter the contents of the object (even by mistake). This avoids unnecessary bugs. Some other immutable objects are integer, float, tuple, and bool.

0

it's okay, please answer to my above comment too

0

methods invoked on immutable objects actually return a copy of the original object.

0

#Paul #I made another sample to show the difference between mutable objects an immutable ones. With mutables the object id stays the same if the object is changed: t="abcde" ID = id(t) print("t", t, ID) t=t.replace("b", "B") print("t", t, id(t), id(t) == ID) #abcde l = [1, 2] ID= id(l) print("l", l, ID) l.append(3) print("l", l, id(l), id(l) == ID)

0

As every character of any string in Python has an index so in case of <. replace()> it iterates thought all elements by their index and try to match with given 2nd string's characters and filter the target element and store it then it rejoins the characters again. So in this case the previous string Doesn't get mutated. It creates new stiring.

0

replace(<old str>,<new str>,<no.of times>)

0

It returns a new string with the changes made.

0

How to understand the python

-1

I agree with both of you Kuba Siekierzyński and 李立威

-2

It's okay bro, it makes a copy, but how does it changes the copy too?