Back to Freecodecamp

Step 8

curriculum/challenges/english/blocks/workshop-caesar-cipher/680a836235461fce1ff53b3f.md

latest1.2 KB
Original Source

--description--

The translate() method takes as argument the translation table generated by str.maketrans(). It is called on a string and returns a copy of the original string where the characters have been replaced based on the translation table:

py
t = str.maketrans('lk', 'br')
sentence = 'The tent gave in to the leaks.'

print(sentence.translate(t))
# Output: The tent gave in to the bears.

Call the translate() method on text passing in the translation_table as the argument and assign the result to a variable named encrypted_text.

--hints--

You should have a variable named encrypted_text.

js
({ test: () => assert(runPython(`_Node(_code).has_variable("encrypted_text")`)) })

You should assign text.translate(translation_table) to encrypted_text.

js
({ test: () => assert(runPython(`_Node(_code).find_variable("encrypted_text").is_equivalent("encrypted_text = text.translate(translation_table)")`)) })

--seed--

--seed-contents--

py
alphabet = 'abcdefghijklmnopqrstuvwxyz'
shift = 5
shifted_alphabet = alphabet[shift:] + alphabet[:shift]
translation_table = str.maketrans(alphabet, shifted_alphabet)
text = 'hello world'
--fcc-editable-region--

--fcc-editable-region--