In Python the following code will modify the output
def changeList(string):
strList = list(string)
for i, char in enumerate(string):
if char == 'a':
strList[i] = 'b'
return "".join(strList)
print(changeList("ab"))
prints out "bb" as expected. However the following function:
def changeList(string):
strList = list(string)
for i, char in enumerate(string):
if char == 'a':
char = 'b'
return "".join(strList)
prints out "ab". Why does setting char
work differently? I had thought that char was a pointer to string[i] but it looks like I'm misunderstand whats going on under the hood?