Learning String Manipulation by Building a Cipher - Step 16

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

I got the correct code but I don’t understand the result, why does it shows -1 rather than ‘a’?
Is it related with shift = 3 code?

If the character isn’t found in the string, find() will return -1.

The first letter in text is H. You are looking for this letter referenced as the letter in the zer0 position: text[0]

text = 'Hello World'
        0123456789...

‘H’ does not exist in the alphabet variable:

alphabet = 'abcdefghijklmnopqrstuvwxyz'`

Read all about it: https://www.w3schools.com/python/ref_string_find.asp

https://www.geeksforgeeks.org/python-string-find/

1 Like

Thank you, it makes sense now.

1 Like