Python 字符串替换似乎不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26943256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:12:26  来源:igfitidea点击:

String replace doesn't appear to be working

pythonstringreplace

提问by vatsalya_mathur

I initially tried using =operator to assign value but it returned an error, then I tried using string.replace():

我最初尝试使用=运算符分配值但它返回一个错误,然后我尝试使用string.replace()

encrypted_str.replace(encrypted_str[j], dec_str2[k], 2)

and

encrypted_str.replace(encrypted_str[j], unichr(ord(dec_str2[k]) - 32), 2)

But it is returning the orignal value.

但它正在返回原始值。

Help out as to how to use the replace API properly to give the correct result Also is there any other API that can be used instead of unichr().

关于如何正确使用替换 API 以给出正确结果的帮助还有没有其他 API 可以用来代替unichr().

The encrypted_stris being taken from the user by encrypted_str = raw_input()dec_str2is the freq string being input by user. The issue hardly concerns the variable I want to know if I am using the replcae()API incorrectly as it is giving me unchanged output for encrypted_strCan we use encrypted_str[j]would return a character from the stringto define the sub string for the replace()API. I used encrypted_str.replace(encrypted_str[j], unichr(ord(dec_str2[k]) - 32), 1)max replace 1 instead of 2(as I need just the one replacement).

encrypted_str正在从用户采取的encrypted_str = raw_input()dec_str2是由用户的频率串被输入。这个问题几乎不涉及我想知道的变量我是否replcae()错误地使用了API,因为它给了我不变的输出,encrypted_str我们可以使用encrypted_str[j]将返回字符串中的一个字符来定义replace()API的子字符串。我使用了encrypted_str.replace(encrypted_str[j], unichr(ord(dec_str2[k]) - 32), 1)max replace 1 而不是 2(因为我只需要一个替换)。

The actual operation that I need to be done will be in C as follows: encrypted_str[j] = dec_str2[k] -32.

在实际操作中,我需要做的工作将在C如下: encrypted_str[j] = dec_str2[k] -32

As I am new to python I am trying to find a substitute.

由于我是 python 新手,我正在尝试寻找替代品。

采纳答案by Blckknght

Strings in Python are immutable. That means that a given string object will never have its value changed after it has been created. This is why an element assignment like some_str[4] = "x"will raise an exception.

Python 中的字符串是不可变的。这意味着给定的字符串对象在创建后永远不会更改其值。这就是为什么像这样的元素赋值some_str[4] = "x"会引发异常。

For a similar reason, none of the methods provided by the strclass can mutate the string. So, the str.replacemethod does not work the way I think you expect it to. Rather than modifying the string in place, it returns a new string with the requested replacements.

出于类似的原因,str该类提供的任何方法都不能改变字符串。因此,该str.replace方法并不像我认为您期望的那样工作。它不是在原地修改字符串,而是返回一个带有请求替换的新字符串。

Try:

尝试:

encrypted_str = encrypted_str.replace(encrypted_str[j], dec_str2[k], 2)

If you're going to be making many such replacements, it may make sense to turn your string into a list of characters, make the modifications one by one, then use str.jointo turn the list back into a string again when you're done.

如果您要进行许多这样的替换,将字符串转换为字符列表,逐个进行修改,然后str.join在完成后使用将列表再次转换回字符串可能是有意义的。

回答by grovesNL

Python strings are immutable. This means that a string cannot be modified by a method call as described in your post. You must use an assignment in order to use the returned string from your method call.

Python 字符串是不可变的。这意味着不能通过您帖子中描述的方法调用来修改字符串。您必须使用赋值才能使用方法调用返回的字符串。

For example:

例如:

encrypted_str = encrypted_str.replace(encrypted_str[j], dec_str2[k], 2)

Now encrypted_strcontains the new value.

现在encrypted_str包含新值。