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 trialKatie Elliott
6,189 PointsAssistance with Introducing Lists Challenge Task 2.
Hello Community,
I have found that I am not understanding how to use the str function and index in order to list only the continents that start with "A". I want to know what I am doing wrong so that I can better understand and correct my error. Thank you!
continents = [
'Asia',
'South America',
'North America',
'Africa',
'Europe',
'Antarctica',
'Australia',
]
# Your code here
print("continent:")
for continent in continents:
print(" * " + continent)
print(str(index), "A", str(continent))
1 Answer
Chris Freeman
Treehouse Moderator 68,457 PointsIn Python, a str
can be tested like a read-only list
(known as immutable) where each letter can be accessed by its position. Like a list
, the first letter is at index 0.
As an example:
SyntaxError: invalid character in identifier
>>> name = 'Bermuda'
>>> name[0]
‘B’
>>> if name[0] == 'B':
... print('starts with B')
...
starts with B
To restrict printing to only countries starting with “A”, a similar if
statement could be added above the print
within your for
loop.
Post back if you need more help. Good luck!!!
Katie Elliott
6,189 PointsThank you Chris! Your answer was helpful and I finally understand what needed to be done.
Sean Layton
1,722 PointsSean Layton
1,722 Points