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 trialStephanie Tabor
2,944 PointsHow do you access characters in a string by index?
Someone help! I eventually solved this by printing a new print line for each item in the list that started with A but, I know there is an easier way.
continents = [
'Asia',
'South America',
'North America',
'Africa',
'Europe',
'Antarctica',
'Australia',
]
# Your code here
for continent in continents:
print("*", continent[A])
2 Answers
Michael Cronk
8,970 PointsThis is what i did and i think it was what you were looking for...
continents = [
'Asia',
'South America',
'North America',
'Africa',
'Europe',
'Antarctica',
'Australia',
]
# Your code here
for continent in continents:
if continent[0] == "A":
print("*", continent)
I used an 'if' statement inside the for loop to see if index of 'continent[0]' was == "A"
Michael Cronk
8,970 PointsYes thats basically it, the statement ' if continent[0] == "A" ' means if the first letter of the string (index [0] - because pythons indexing starts at 0) Then print.... So yeah you seem to understand it.
Stephanie Tabor
2,944 PointsStephanie Tabor
2,944 PointsAh, I see. Thank you Michael Cronk!
Kay Fuen
1,144 PointsKay Fuen
1,144 PointsThanks for posting. Super new to this and trying to understand so the "if continent [0] =="A"" reads if continent starts with ([0] place) equals (== A) then print etc? So it reads each continent, first letter, and print all strings in the list?