Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Introducing Lists Using Lists Continental

Grayden Kellogg
seal-mask
.a{fill-rule:evenodd;}techdegree
Grayden Kellogg
Python Development Techdegree Student 1,826 Points

Printing items from list that start with a specific letter?

I have tried a bunch of different code to try and get it to print the continents that start with "A" and I just can't figure it out.

continents.py
continents = [
    'Asia',
    'South America',
    'North America',
    'Africa',
    'Europe',
    'Antarctica',
    'Australia',
]
# Your code here
for continent in continents:
    print("* " + continent)
Grayden Kellogg
seal-mask
.a{fill-rule:evenodd;}techdegree
Grayden Kellogg
Python Development Techdegree Student 1,826 Points

This is my code that printed out an asterik followed by the continent name. Next step is to print out just the ones that start with "A". That's where I'm having trouble

4 Answers

Steven Parker
Steven Parker
231,269 Points

Now you need to add a conditional (if) between the loop and the print. The conditional will test if the first letter of the word is the desired one.

And one way to isolate a specific letter is by using indexing (with brackets [ ]).

Grayden Kellogg
seal-mask
.a{fill-rule:evenodd;}techdegree
Grayden Kellogg
Python Development Techdegree Student 1,826 Points

I have been trying indexing and if statements and I just can't figure out how to state it the right way in my code. The indexes have to be an integer correct?

Steven Parker
Steven Parker
231,269 Points

That's right. And index values start at 0, so the first letter of continent would be continent[0].

John Hill
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
John Hill
Front End Web Development Techdegree Graduate 35,236 Points

Hey Grayden, if you got the first part of the quiz right, then you know how to set up for/in loops. All you're doing in the second part of the quiz is adding an if-statement like so:

for continent in continents:
    if continent[0] == "A":
        print("* "+ continent)

I don't get how to say if the first letter of a string is an "A" than print. I know i need to use indexes

continent[0] pulls the first letter of the string and compares it with a double equals sign to string value "A". Remember that indexing starts at 0, so the first letter is called with [0]. The only other things to watch out for here are remembering the colons and to indent correctly.

Laura Mora Freeman
Laura Mora Freeman
8,192 Points

Thank you John. I have another question, if you would like to call an entire item of the list, lets say "Asia¨, not just one letter, how would you call it?

Steven Parker
Steven Parker
231,269 Points

Inside the loop, each item is simply "continent" (with no brackets).

Grayden Kellogg
seal-mask
.a{fill-rule:evenodd;}techdegree
Grayden Kellogg
Python Development Techdegree Student 1,826 Points

I had the 2nd part correct but I was only using one "=" sign instead of two when i wanted to test equality. Silly mistake but I got past that. Thanks for the help and comments

Ravi Batra
Ravi Batra
2,486 Points

Thank you so much. I looked all over for this

Steven Parker
Steven Parker
231,269 Points

The equality test operator is double equal sign (==). Are you able to get it to pass now?

Hi, I thought 0 would pull up the first object in the list ("Asia"), and not just the first letter. Is it because we are calling continent and not continents?

Steven Parker
Steven Parker
231,269 Points

That's right. The loop puts just one word at a time in "continent".