+ 14

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!!!

3rd Jun 2018, 8:40 AM
Hemath Kumar
Hemath Kumar - avatar
19 odpowiedzi
+ 18
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.
3rd Jun 2018, 9:35 AM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 34
replace() actually returns a *copy* of the argument string with relevant parts replaced. The original string is left intact.
3rd Jun 2018, 8:50 AM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 22
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.
3rd Jun 2018, 8:52 AM
李立威
+ 8
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))
3rd Jun 2018, 11:37 AM
Paul
Paul - avatar
+ 4
Strings are immutable it means we that can't make changes in place. Example: string="hello world!" string [0]='p' #this statement will result in error But we can change it as a whole i.e. string="welcome!" So replace() function can work here
27th Feb 2022, 9:43 AM
Jaya Vishwakarma
Jaya Vishwakarma - avatar
+ 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.
3rd Jun 2018, 9:01 AM
李立威
+ 1
methods invoked on immutable objects actually return a copy of the original object.
13th Jun 2018, 7:37 PM
Onyedikachi Nnadi
Onyedikachi Nnadi - avatar
+ 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.
14th Sep 2021, 7:23 AM
T.M.Sarabesh Kannaa
T.M.Sarabesh Kannaa - avatar
0
it's okay, please answer to my above comment too
3rd Jun 2018, 8:54 AM
Hemath Kumar
Hemath Kumar - avatar
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)
17th May 2021, 8:06 AM
Tima
0
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))
19th Jun 2021, 2:43 AM
Abdelrahman shaheen
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.
2nd Jul 2021, 1:40 PM
Sougata Jana
Sougata Jana - avatar
0
replace(<old str>,<new str>,<no.of times>)
18th Jul 2021, 4:54 AM
Vaishnavi Gupta
0
It returns a new string with the changes made.
15th Sep 2021, 6:17 PM
Luke Amos
Luke Amos - avatar
0
How to understand the python
27th Sep 2021, 7:54 AM
Melerose Tandog
Melerose Tandog - avatar
0
Yes once a string is created we can't change the value of string But we can change the variable to other value string has many methods that can perform operations / changes on it and return the result Ex : a = "SanDeeP" a = a.lower() print(a) Output : sandeep
13th Jul 2022, 4:27 PM
Konda Sandeep
Konda Sandeep - avatar
- 1
I agree with both of you Kuba Siekierzyński and 李立威
3rd Jun 2018, 9:36 AM
Hemath Kumar
Hemath Kumar - avatar
- 2
It's okay bro, it makes a copy, but how does it changes the copy too?
3rd Jun 2018, 8:52 AM
Hemath Kumar
Hemath Kumar - avatar