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.

Grayden Kellogg
Python Development Techdegree Student 1,692 PointsPrinting 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 = [
'Asia',
'South America',
'North America',
'Africa',
'Europe',
'Antarctica',
'Australia',
]
# Your code here
for continent in continents:
print("* " + continent)
3 Answers

Steven Parker
222,324 PointsNow 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
Python Development Techdegree Student 1,692 PointsI 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
222,324 PointsThat's right. And index values start at 0, so the first letter of continent
would be continent[0]
.

John Hill
Front End Web Development Techdegree Graduate 35,236 PointsHey 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
8,151 PointsThank 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
222,324 PointsInside the loop, each item is simply "continent" (with no brackets).

Grayden Kellogg
Python Development Techdegree Student 1,692 PointsI 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
1,995 PointsThank you so much. I looked all over for this

Grayden Kellogg
Python Development Techdegree Student 1,692 PointsI 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
222,324 PointsThe equality test operator is double equal sign (==
). Are you able to get it to pass now?
Grayden Kellogg
Python Development Techdegree Student 1,692 PointsGrayden Kellogg
Python Development Techdegree Student 1,692 PointsThis 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