-1

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:

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.

Akaisteph7
  • 5,034
  • 2
  • 20
  • 43
Hikio
  • 3
  • 1
  • 1
    `phrase =phrase.replace("f", "g").replace("e", "f").replace("d", "e").replace("c", "d").replace("b", "c").replace("a", "b")` – depperm Jul 09 '19 at 18:19
  • "it does not work" is not specific enough. Are you getting error messages? Or unexpected output? – ForceBru Jul 09 '19 at 18:19

5 Answers5

2

For python 3 you can use str.maketrans and str.translate (links below) - for python 2 you can find them inside the string module:

# 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

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
0

You have to replace the string with the output of phrase.replace(). Since phrase is a string, it does not get updated as strings are immutable. You also have to change the order of your replacements or you get gggggg as a result:

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
Akaisteph7
  • 5,034
  • 2
  • 20
  • 43
0

the replace function returns a new string that is the copy of the previous one with the characters replaced, so your code should be, as @depperm mentioned:

phrase = phrase.replace("f", "g").replace("e", "f").replace("d", "e").replace("c", "d").replace("b", "c").replace("a", "b")
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
0

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:

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'

Ropoja
  • 1
  • 2
0

If you're trying to learn python a great tool to use in the python repl. In this case there's nothing wrong with the replace method, it's your logic that's incorrect. So let's see what's going on in the 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.