Back to Freecodecamp

Step 6

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

latest1.4 KB
Original Source

--description--

The str.maketrans() method takes two strings of equal length and returns a translation table that maps each character of the first string with the corresponding character of the second string. Each character in the translation table is stored as a Unicode ordinal, a number that uniquely identifies the character.

py
lower_chars = 'abc'
upper_chars = 'ABC'

table = str.maketrans(lower_chars, upper_chars)
print(table) # {97: 65, 98: 66, 99: 67}

You'll learn more about the type of structure returned by str.maketrans() later on in the curriculum. For now, create a translation table that maps the characters in alphabet to the characters in shifted_alphabet and assign it to a variable named translation_table.

--hints--

You should declare a variable named translation_table.

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

You should call str.maketrans() passing in alphabet and shifted_alphabet as the arguments, and assign the result to translation_table.

js
({ test: () => assert(runPython(`_Node(_code).find_variable("translation_table").is_equivalent("translation_table = str.maketrans(alphabet, shifted_alphabet)")`)) })

--seed--

--seed-contents--

py
alphabet = 'abcdefghijklmnopqrstuvwxyz'
shift = 5
shifted_alphabet = alphabet[shift:] + alphabet[:shift]
--fcc-editable-region--

--fcc-editable-region--