How does indexing a string work? or What does `word[0]` in the example mean?

Not really. word in that case would be an iterable character sequence, a str type, not a number.

Oops, you are right. Thank you mtf.

1 Like

So, as the loop runs the index 0 in word[0] increases?

word[0]
word[1]
word[2]
etc.

How?

Wait, word[0] is always word[0] because it is checking the first index of the string. I think I understand it now.

Does variable name need to be the same as the element name?

E.g.- the following code gave me an error:

canride for can_ride in ages: canride = []

the syntax of the for loop is:

for element in list:

not sure why you have something before your for keyword

This is the code from the list comprehension exercise which showed an error:

heights = [161, 164, 156, 144, 158, 170, 163, 163, 157]
can_ride_coaster=[canride for can in heights if can>161 ]
print(can_ride_coaster)

canride is undefined. What should be appended to the list?

Not to necropost, but this information would be really helpful to include in the example of the exercise. Don’t believe we’ve been shown that you can index individual characters in a string so it’s pretty confusing at first glance; makes it much harder to absorb the concept behind the initial list comprehension when that function is utilized without any introduction.

3 Likes

Agree. Did someone different write this section? It appears to lack the attention to detail that previous sections contain throughout.

The inclusioin of the [0] in the example only confused me and did not help me understand this concept–in fact it did just the opposite. Please improve this section.

Hi @stetim94. Thanks, this explanation was the one that clicked for me. But just to be sure, are you saying that for ‘word[0] == @’, the 0 index is referring to the first letter of each word in the list? And say if I put ‘word[2] == @’ it would be checking to see if the 3rd character in each word in the list was @?

If so that makes sense, but I wish it were actually explained in the module itself. Thanks!

that is absolutely right :slight_smile:

I had the same confusion right now.