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 trialRyan Stjernholm
452 PointsHow do I print characters from a string using index?
Printing the continents that start with an "A"?
continents = ['Asia', 'South America', 'North America', 'Africa', 'Europe', 'Antarctica', 'Australia']
print("continents:")
for continent in continents:
print("* " + continents[0])
2 Answers
Steven Parker
231,248 PointsYou don't want to just print the first letter, so leave the print statement as it was from the first task.
But you can put an "if" statement in front of it, to control when to print a name. The "if" statement can check to see if the first letter is what the instructions ask for.
Oskar Lundberg
9,534 PointsIn order to print characters from a string using index, you can do just like you would do if you wanted to print an item from a list (using index). let's say we have the variable fruit = "Apple" We can then use fruit[0] to get back "A"
When it comes to your code challenge, you can add an 'if' statement so it only prints out the continents where the first letter is A. I did as follows:
continents = ['Asia', 'South America', 'North America', 'Africa', 'Europe', 'Antarctica', 'Australia']
for continent in continents:
if continent[0] == "A":
print("* " + continent)
Remember to use == when comparing two values :)
Ryan Stjernholm
452 PointsRyan Stjernholm
452 Pointsif continents = "A" - ...... is this along the lines of correct?
Steven Parker
231,248 PointsSteven Parker
231,248 PointsHere's a few extra hints:
continent[0]
")