为什么第一个代码中的替换函数不起作用?

huangapple go评论77阅读模式
英文:

Why the replace function does not work in the first code?

问题

Code 1

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

Output:

He had a crazy, crazy dream!

我想在代码1中用"strange"替换"crazy"。但实际上,它返回了原始消息,没有修改它。

Code 2

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

Output:

He had a strange, strange dream!

现在,在我分配了变量b并打印b之后。

代码1中的问题是你没有将替换后的字符串存储回变量messagestr.replace() 函数不会改变原始字符串,而是返回一个新的字符串,包含了替换后的内容。所以,要修复代码1,你需要将message重新赋值为替换后的字符串,就像代码2 中一样。

英文:

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?

答案1

得分: 3

在代码1中,replace() 方法被调用在消息字符串上,但方法的返回值没有保存回消息变量。在Python中,字符串是不可变的,这意味着任何修改字符串的方法实际上返回一个具有应用修改的新字符串,而不是直接修改原始字符串。

所以在代码1中,replace() 方法创建了一个新字符串,将 "crazy" 替换为 "strange",但这个新字符串没有被保存在任何地方,所以原始消息字符串保持不变。

在代码2中,replace() 方法的返回值被保存到一个新变量 b 中,该变量包含了被替换为 "strange" 的修改后的字符串。然后正确地打印出了修改后的字符串。

要修复代码1,并将消息字符串中的 "crazy" 替换为 "strange",您可以将replace()方法的返回值保存回消息变量,像这样:

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

这将在消息字符串中将 "crazy" 替换为 "strange",然后将修改后的字符串保存回消息变量。输出将是:

He had a strange, strange dream!
英文:

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!

答案2

得分: 0

因为在替换函数中,它返回替换字符串后的字符串,而不是在原地替换(即在调用它的同一字符串上进行替换)。这就是为什么第一个代码中它不起作用,而在第二个代码中,变量 b 存储了返回的结果,因此它起作用。希望这解决了疑惑。

英文:

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.

huangapple
  • 本文由 发表于 2023年3月12日 13:41:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75711251.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定