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 trialGrayden Kellogg
Python Development Techdegree Student 1,826 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)
4 Answers
Steven Parker
231,275 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,826 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
231,275 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,192 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
231,275 PointsInside the loop, each item is simply "continent" (with no brackets).
Grayden Kellogg
Python Development Techdegree Student 1,826 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
2,486 PointsThank you so much. I looked all over for this
Grayden Kellogg
Python Development Techdegree Student 1,826 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
231,275 PointsThe equality test operator is double equal sign (==
). Are you able to get it to pass now?
Sara Truyts
956 PointsHi, 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
231,275 PointsThat's right. The loop puts just one word at a time in "continent".
Grayden Kellogg
Python Development Techdegree Student 1,826 PointsGrayden Kellogg
Python Development Techdegree Student 1,826 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