0

Code 1

message = "He had a crazy, crazy dream!"
message.replace("crazy","strange")
print(message)

Output:

He had a crazy, crazy dream!

I want to replace the word "crazy" with "strange" in code 1. But instead it returns the original message. It does not modify it.

Code 2

message = "He had a crazy, crazy dream!"
b = message.replace("crazy","strange")
print(b)

Output:

He had a strange, strange dream!

Now after I assigned b variable and print b.

What is wrong with my code 1? Why doesn't the replace function work?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • 1
    [`replace`](https://docs.python.org/3/library/stdtypes.html#str.replace) _Return a copy of the string_. In code 2, you are assigning this copy to `b`. In code 1 you do nothing with the copy and print the original string. – Ignatius Reilly Mar 12 '23 at 05:46
  • 2
    Does this answer your question? [String replace doesn't appear to be working](https://stackoverflow.com/questions/26943256/string-replace-doesnt-appear-to-be-working) – ARK1375 Mar 12 '23 at 05:47
  • 1
    Does this answer your question? [Why doesn't calling a string method (such as .replace or .strip) modify (mutate) the string?](https://stackoverflow.com/questions/9189172/why-doesnt-calling-a-string-method-such-as-replace-or-strip-modify-mutate) – Ignatius Reilly Mar 12 '23 at 05:49

2 Answers2

3

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!
macwray17
  • 31
  • 3
0

because in replace function returns the string with replacing the strings

it does not replace in place (i.e. at the same string on which it is called)

that's why it is not working in first code and in second code b stores the returned result to it is working

hope this solves the doubt.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – xlmaster Mar 17 '23 at 17:28