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 to run an if on a string

I am doing the challenge "Continental" and it is telling me to find the continents that begins with "A". How do I do this? Thank you

continents.py
continents = [
    'Asia',
    'South America',
    'North America',
    'Africa',
    'Europe',
    'Antarctica',
    'Australia',
]
# Your code here
for continent in continents:
    if continent > "A":
        print("* " + continent)

1 Answer

Alex Koumparos
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Koumparos
Python Development Techdegree Student 36,887 Points

Hi yc5,

Strings behave, in most respects, like lists. For example, just like we could do this with a list:

my_list = ['h', 'e', 'l', 'l', 'o']
my_list[1]  # will access the element at index 1, i.e., 'e'

We can do the same thing with a string:

my_string = "hello"
my_string[1]  # will access the character at index 1: 'e'

Now we know how to access an element at a position, we can put that in the if statement:

my_string = "hello"
if my_string[1] == "e":
    print("the character is 'e'")

Often, when dealing with strings, we want our tests to be case insensitive. One way we can do this is to ensure that regardless of the case we are being passed, we only look at the lowercase version of it:

my_string = "hELlO"
if my_string[1].lower() == "e":
    print(my_string[1])

Hope that clears everything up for you

Cheers

Alex