educative.io

Append and replace

Why ther eis requirement if assignment to make replace function work like this
“”"
four = four.replace(“0b”,"")
“”"
while in lists, a simple .append does the work without reassigning.


Type your question above this line.

Course: https://www.educative.io/collection/10370001/5474278013140992
Lesson: https://www.educative.io/collection/page/10370001/5474278013140992/5442674939133952

Hello @Mohammad_Alim

This behavior is due to the internal working of Python. .append method of the list works in-place whereas the .replace method does not. Hence, to reflect the changes in the original variable that is four, we have to re-assign it the replaced value.

Hope this helps!