简体   繁体   English

如何正确替换字符串中的字符?

[英]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.maketransstr.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.

相关问题 如何在Python中替换字符串中的字符? - How do I replace characters in a string in Python? 在 Pandas Python 中,如何替换字符串列中的字符? - In Pandas Python how do i replace Characters in String column? 如何替换 Python 字符串中的所有字符? - How do I replace all characters in a Python string? 如何使用字符映射表替换字符串中的字符? - How do I replace characters in a string using a character map? 如何将字符串拆分为字符并用浮点值替换字符以找到 Python 中原始字符串的总和? - How do I split string into characters and replace characters with float values to find the sum of original string in Python? 如何用替换字符替换列表中字符串中的所有特定字符? - How do I replace all of the specific characters in the string from the lists with the replacement characters? 如何在Python中正确使用“替换”方法处理一组字符 - How can I use the “Replace” method correctly for a set of characters in Python 我如何遍历字符串并正确替换python中的特定字符? - How do i iterate through a string and replace specific chars in python correctly? 如何使用.replace()函数将字符串的最后4个字符(最后4个字符除外)更改为#符号? - How do I use the .replace() function to change all but the last 4 characters of a string into the # symbol? 如何在Python 3.5中为使用随机替换字符串中字符的函数编写doctest? - How do I write a doctest in python 3.5 for a function using random to replace characters in a string?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM