Scientific Computing with Python - Step 18

For this step:

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

why the result is 7 not 8? as there are 8 lowercase in Hello World

Can you describe what you think this line does in your own words?

find lower case in alphabet in variable index. Now i’m more confuse.

similar letters don’t count.
h.e.l.o.w.r.d

I don’t think you quite get what that line does.

What is text[0].lower()?

to change them as a lower case? then find them in alphabet variable?

What is “them” though? text[0].lower() only converts one thing into lowercase.

convert H to h and W to w? so it becomes hello world instead of Hello World, then match them to the variable of alphabet.

Nope.

Lets start with something easier.

What is text[0]

text[0] is a string that start with first letter H.
It started from 0

Can you be a bit more specific? What is the output of this code:

text = 'Hello World'
print(text[0])

output: H
H = 0
e = 1
l = 2
l = 3
o = 4

W = 5
o = 6
r = 7
l = 8
d = 9

Exactly, yes. Well just the first part.

And what’s the output of this:

text[0].lower()

output = -1
because it wasn’t a lower case.

What does the .lower() method do?

What is the output of this:

print("Hello".lower())

Test it out in the editor if you aren’t sure of the answer. You can test and confirm all of these questions yourself, it’s an open book test.

.lower() is to change all letters to be lowercase.
output = hello ?

where is the editor?

Yes, now what is the output of this:

text[0].lower()

The editor is where you type the code for Step 18 that you’re working on.

You could also test in a different editor:
https://www.w3schools.com/python/trypython.asp?filename=demo_ref_string_lower

x = text[0].lower()
print(x)

output = h

1 Like

Yes, got it. Now what is the output of this:

alphabet = 'abcdefghijklmnopqrstuvwxyz'
print(alphabet.find('a'))

I hope you are seeing the connections now. You can always experiment with this in the editor and print out commands to see what they do. You have to do this or you are walking around blind.

yes, I did the logic first, when I can’t find the logic, I tested out in the editor. For this, back to basic, the output is 0 ?

1 Like