Learn String Manipulation by Building a Cipher - Step 16

Tell us what’s happening:

I don’t get this, can anyone help me

Your code so far

/* User Editable Region */

text = ‘Hello World’
shift = 3
alphabet = ‘abcdefghijklmnopqrstuvwxyz’
index = alphabet.find(text[0].lower())
print(index)
index+shift

/* User Editable Region */


### Your browser information:

User Agent is: <code>Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36</code>

### Challenge Information:
Learn String Manipulation by Building a Cipher - Step 16
https://www.freecodecamp.org/learn/scientific-computing-with-python/learn-string-manipulation-by-building-a-cipher/step-16

Do you remember how to access a letter position of a string? text[0] accesses the letter at the first (0) position of text.

You want to access a letter in the string alphabet at the position index + shift

Try these steps:

  • declare a variable named shifted and assign it the string alphabet
  • modify the assignment into alphabet at position index using square brackets
  • modify the position in the square brackets to index + shift

so in this case, I need to add the letter e and d in the alphabet string, right? To make the word “shifted”

You know if you want to access the h in alphabet would be alphabet[7] since you already found the position. Now you want to slide 3 (stored in the shift variable) letters down the alphabet from h and get that letter.

Let’s go step by step

Show me this step

So shifted is the new variable, right?

Yes, exactly shifted is the new variable to assign to

index = alphabet.find(text[h].lower()) so for this, it only give you one letter, right?

shifted = shift + index = alphabet.find(text[e].lower()) + index = alphabet.find(text[d].lower())

Just do the first step first:

declare a variable named shifted and assign it to alphabet

This problem has been tripping up many people, I promise we’ll get it right now though, just need to follow 3 steps

uhm, but declare the word “shifted” based on what?

Here you declared the variable text and assigned it 'hello world`
you declared shift and assigned it 3

now declare shifted and assign it alphabet, so it will equal the variable string alphabet

oh so,
shifted = alphabet ?

Exactly. If you print(shifted) it would be “abcdefghijklmnopqrstuvwxyz”

Now: modify alphabet to access position index using square brackets, just as you did with text[0] but instead of position 0 it will be position index

it is kinda not enough information

thats step 1 of 3, keep going

but why do they want to assign shift = 3?

This how the cipher scrambles a sentence, it shifts each letter 3 positions down the alphabet. abc becomes def

index = alphabet.find(text[h].lower())
so this mean the index value is a letter, right?

i thought this is beginner level

index = alphabet.find(text[0].lower())
Do you mean this line? text[h] isn’t really meaningful. The square brackets are used to access a position in the string by index number. 0, 2, 6.

index will be a number, representing the position of a letter in alphabet