Learn String Manipulation by Building a Cipher - Step 16

Tell us what’s happening:

I can’t seem to understand how to do this, no matter what I type I always get it wrong. I’ve watched several videos on indexing and searched google but everything I look at is different than what I’m trying to do so I can’t figure it out.

Your code so far


/* User Editable Region */

text = 'Hello World'
shift = 3
alphabet = 'abcdefghijklmnopqrstuvwxyz'
index = alphabet.find(text[0].lower())
print(index)
shifted = alphabet[7] + shift[3]

/* User Editable Region */

Your browser information:

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

Challenge Information:

Learn String Manipulation by Building a Cipher - Step 16

Instead of using hard coded values like 7 or 3, follow the instruction to add index and shift like this.

shifted=alphabet[index+shift]

but how does that work? I tried using round brackets, and no brackets before to add index and shift together but it didn’t work. Why use square brackets and not round brackets? I thought we only used square brackets for the numbers we are looking for. what is a hard coded value? is a hard coded value an integer or something i’ve defined?

number = 5
text[0]         #returns the 1st character "t"
text[number]    #returns the character at index 5, "r" (index starts at 0)
text[number + 5]    #returns the character at index 10 (error)

Square brackets is how to access an element in an object (string, list etc).

text[number + 5] number is a variable. 5 is hard-coded. You want to avoid hard coded numbers like this because they cannot change dynamically.

If you want to access each character in text in a loop, you want text[number] and number will keep changing from 0,1,2,3,4,5. Hard coding it would be like this:

text[0]
text[1]
text[2]
text[3]

More reading:

https://www.freecodecamp.org/news/python-variables/

https://www.freecodecamp.org/news/python-string-manipulation-handbook/#pythonstringbasics

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.