Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Introducing Lists Using Lists Continental

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)

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

3 Answers

Steven Parker
Steven Parker
222,324 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 [ ]).

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
222,324 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,151 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
222,324 Points

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

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
1,995 Points

Thank you so much. I looked all over for this

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

Steven Parker
Steven Parker
222,324 Points

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