In Code 1, the replace() method is being called on the message string, but the return value of the method is not being saved back to the message variable. Strings in Python are immutable, which means that any method that modifies a string actually returns a new string with the modifications applied, rather than modifying the original string in place.
So in Code 1, the replace() method creates a new string with "crazy" replaced by "strange", but this new string is not saved anywhere, so the original message string is printed unchanged.
In Code 2, the return value of the replace() method is saved to a new variable b, which contains the modified string with "crazy" replaced by "strange". The modified string is then printed out correctly.
To fix Code 1 and replace "crazy" with "strange" in the message string, you can save the return value of the replace() method back to the message variable, like this:
message = "He had a crazy, crazy dream!"
message = message.replace("crazy","strange")
print(message)
This will replace "crazy" with "strange" in the message string, and then save the modified string back to the message variable. The output will be:
He had a strange, strange dream!