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 Types and Branching Comparisons

Patrick Poplawska
Patrick Poplawska
8,826 Points

"is" operator is returning True unexpectedly

Interesting behavior (in REPL): favorite_color = 'red' favorite_color is 'red' returns True.

Is this because Python is doing some clever object management in the background, and not creating a new string object containing the value 'red' when I execute the branch expression?

1 Answer

Patrick Poplawska
Patrick Poplawska
8,826 Points

Did a quick search, and by using the id() function I am able to confirm that (even when assigning to another variable) that this behavior is happening because it points to the same address in memory. The variable portion is a consequence of strings being immutable.