Welcome to the Treehouse Community
Looking to learn something new?
Anthony Lopez963 Points
Stuck on a question in Introducing Lists!
I'd like you to now only print continents that begin with the letter "A".
HINT: Remember that you can access characters in a string by index
continents = ['Asia,' etc.]
How do I call only the continents in that list that start with the letter A?
I'm not sure how to write it out:
for continent in continents: ????
continents = ['Asia', 'South America', 'North America', 'Africa', 'Europe', 'Antarctica', 'Australia'] # Your code here for continent in continents: print("A")
For this challenge, it's important that you know how to access characters in a string by index. The first character of a string is at index position 0.
my_string = "Asia" print(my_string) # this gives me back the letter A
Now you could use this to check if a continent starts with the 'A', and if so, print it out:
for continent in continents: if continent == "A": print("* " + continent)
Correct! If you asked for index position 0 on the continents list, you'll get back the first item from that list. In this case that's indeed 'Asia.'
But that's not what we asked in this for loop! Maybe it's a little bit more clear if we write it like this:
for item in continents: # loop over each item in the continents list if item == "A": print("* " + item)
Item stands for each individual item in the list (all the continent names). So item gives us back the first character of that item. Just like my_string did in the example above.
Hopefully I was able to answer your question!