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 Python Basics Functions and Looping For Looping

I do not understand why it would print out o o h

What is the output of the following code?

for letter in "You got this!":
    if letter in "oh":
        print(letter)

1 Answer

Hazem mosaad
Hazem mosaad
15,384 Points

Hello ! as you see what the loop is doing the following

# when loop start for  first time  ..  take first letter y and then create the check 
if y in "oh"  # false because y not in oh so nothing happen then.
if o in "oh"  # true because o in oh so print "o" to screen then.
#loop through all letters until reach another "o" so print it.
#until now the output is "oo" when reach one "h" and "h" in "oh" so the final output is "ooh".

I hope this clear for you happy programming !