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

.lower() and .upper() in if: else: statements

I am a little confused with the logic of .lower() in the if: statement here. if should_proceed.lower() == "y" I understand what is being done, but am just confused on when to use .lower() vs. .upper().

So if we add .lower() , uppercase input will be accepted. if I change it to .upper(), uppercase input is not accepted.

This just seems backwards to me. What are .lower() and .upper() doing "under the hood". How do I know when to use them?

Also, is there just another function that can be used to cover all case discrepancies?

Thanks for taking the time to answer. That helps.

1 Answer

Travis Alstrand
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Travis Alstrand
Treehouse Teacher

Hey there Karen Kozlosky !

I guess that this would all depend on what your preference is. It sounds like this is checking against some user input. So by adding .lower() it's going to work whether the user has typed in Y or y. You could do the same with upper in this scenario and instead check

if should_proceed.upper() == "Y"

If you were dealing with, for example, a user's search input, it would then depend on how the data we're searching through is stored. Most of the time with things like this it would be all lowercase or how we would normally write & capitalize things. If the latter is the case, then we'd simply have to add .lower() or .upper() to both the user's input and the string we're comparing it to.

I hope this helps clear things up! :smiley: