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

Carlos Alvarez
Carlos Alvarez
2,205 Points

Printing labels with certain letters

I'm drawing a huge blank on how to print certain labels that have a specific letter in the start. I've looked at my notes and can't figure it out.

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

3 Answers

Exactly what Megan said! This would be a great time to use an 'if' statement so let's say you have a list that you only want to display the names starting with M:

names = [
    'Mel',
    'Carlos',
    'Megan',
    'Mary',
    'Joe',
    'Alex'
]

for name in names:
    if name[0] == "M":
        print(f"* {name}")

This would output

  • Mel
  • Megan
  • Mary
Megan Amendola
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree seal-36
Megan Amendola
Treehouse Teacher

You can access specific characters in a string just like you access items in a list with indexing. For example, if I wanted to access the second character in animal = 'cat', I would print(animal[1]).

Carlos Alvarez
Carlos Alvarez
2,205 Points

Thank you both so much I wrote down the correct code I just didn't understand it until now.