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

How do I print characters from a string using index?

Printing the continents that start with an "A"?

continents.py
continents = ['Asia', 'South America', 'North America', 'Africa', 'Europe', 'Antarctica', 'Australia']
print("continents:")
for continent in continents:
    print("* " + continents[0])

2 Answers

Steven Parker
Steven Parker
231,269 Points

You 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.

if continents = "A" - ...... is this along the lines of correct?

Steven Parker
Steven Parker
231,269 Points

Here's a few extra hints:

  • a single "=" is an assignment, an equality comparison uses a double symbol ("==")
  • "continents" (plural) is the entire list
  • the first letter of one "continent" can be accessed using an index of 0 ("continent[0]")
Oskar Lundberg
Oskar Lundberg
9,534 Points

In 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 :)