[英]How do I correctly replace characters in a string?
I am new to programming in general, and couldn't figure out how to replace multiple characters in a string. 我一般对编程都不熟悉,无法弄清楚如何替换字符串中的多个字符。 Using the string.replace("x", "y")
function, I tried to make a simple encoder: 使用string.replace("x", "y")
函数,我尝试制作一个简单的编码器:
phrase = "abcdef"
phrase.replace("a", "b")
phrase.replace("b", "c")
phrase.replace("c", "d")
phrase.replace("d", "e")
phrase.replace("e", "f")
phrase.replace("f", "g")
print(phrase)
I was expecting the output to be: 我期望的输出是:
"bcdefg"
but instead got 但是得到了
"abcdef"
This was best I could come up with and it does not work. 这是我能想到的最好的方法,它不起作用。 I looked around other questions and the answers were too confusing. 我环顾了其他问题,答案太混乱了。 Please help and explain what i am doing wrong. 请帮助并解释我在做什么错。
For python 3 you can use str.maketrans
and str.translate
(links below) - for python 2 you can find them inside the string
module: 对于python 3,您可以使用str.maketrans
和str.translate
(下面的链接)-对于python 2,您可以在string
模块中找到它们:
# from string import maketrans, translate # python 2, in python 3 they are on str
trans = str.maketrans("abcdef","bcdefg")
phrase = "abcdef"
print(str.translate(phrase, trans)) # outputs: bcdefg
See 看到
You have to replace the string with the output of phrase.replace()
. 您必须将字符串替换为phrase.replace()
的输出。 Since phrase
is a string, it does not get updated as strings are immutable. 由于phrase
是字符串,因此不会更新,因为字符串是不可变的。 You also have to change the order of your replacements or you get gggggg
as a result: 您还必须更改替换顺序,否则将导致gggggg
:
phrase = "abcdef"
phrase = phrase.replace("f", "g")
phrase = phrase.replace("e", "f")
phrase = phrase.replace("d", "e")
phrase = phrase.replace("c", "d")
phrase = phrase.replace("b", "c")
phrase = phrase.replace("a", "b")
print(phrase)
bcdefg
replace函数返回一个新字符串,该字符串是前一个字符替换后的字符串的副本,因此您的代码应为@depperm所述:
phrase = phrase.replace("f", "g").replace("e", "f").replace("d", "e").replace("c", "d").replace("b", "c").replace("a", "b")
Just made a quick search for you and found this Change each character in string to the next character in alphabet 刚刚对您进行了快速搜索,发现了这一点将字符串中的每个字符更改为字母中的下一个字符
where @dawg put this: @dawg放在哪里:
from string import ascii_lowercase as letters
s='abcxyz'
ns=''
for c in s:
if c in letters:
ns=ns+letters[(letters.index(c)+1)%len(letters)]
else:
ns+=c
Tested it and worked fine, ns value is 'bcdyza' 经过测试并正常工作, ns值为“ bcdyza”
If you're trying to learn python a great tool to use in the python repl. 如果您想学习python,那么可以在python repl中使用的好工具。 In this case there's nothing wrong with the replace method, it's your logic that's incorrect. 在这种情况下,replace方法没有任何问题,这是您的逻辑不正确。 So let's see what's going on in the repl. 因此,让我们看看REPL中发生了什么。
>>> phrase = "abcdef"
>>> phrase.replace("a", "b")
'bbcdef'
>>> phrase.replace("b", "c")
'accdef'
>>> phrase.replace("c", "d")
'abddef'
>>> phrase.replace("d", "e")
'abceef'
>>> phrase.replace("e", "f")
'abcdff'
>>> phrase.replace("f", "g")
The correct algorithm for this kind of caesar cipher would be something like. 这种凯撒密码的正确算法将是类似的。
s = ''
for i in range(len(phrase)):
s += chr(ord(phrase[i])+1)
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.