18 Réponses
+ 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.
+ 34
replace() actually returns a *copy* of the argument string with relevant parts replaced.
The original string is left intact.
+ 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.
+ 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))
+ 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
+ 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
methods invoked on immutable objects actually return a copy of the original object.
+ 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
#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
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))
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
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
- 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?