Python Introducing Lists Using Lists Continental

Stephanie Tabor
Stephanie Tabor
2,942 Points

How 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.py
continents = [
    'Asia',
    'South America',
    'North America',
    'Africa',
    'Europe',
    'Antarctica',
    'Australia',
]
# Your code here
for continent in continents:
    print("*", continent[A])

2 Answers

Michael Cronk
Michael Cronk
5,550 Points

This 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"

Kay Fuen
Kay Fuen
1,144 Points

Thanks 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?

Michael Cronk
Michael Cronk
5,550 Points

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