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 trialLukas Johmann
911 PointsPurpose of the ".lower" function
Can someone explain what exactly the purpose of the ".lower" function is. Which ability does it give the input in our code?
for example here:
should_proceed = input("Do you want to proceed? Y/N ")
if should_proceed.lower == "y":
2 Answers
Ivan Kazakov
Courses Plus Student 43,317 PointsBraces after the .lower
should indeed be used in this context, like this: .lower()
. Otherwise the expression is evaluating whether the .lower
method of str
type equals to y
.
Since Python is case-sensitive, using .lower()
would allow to evaluate that expression to True
for both Y
and y
. And to False
for everything else.
Travis Whiteman
3,342 PointsAn example of this could be used in a rock-paper-scissor game. In my python class at my college, we had to create a command line rock-paper-scissor game. So in my if statement I had the following code:
if player.lower() == computer:
print("Tie\n")
elif player.lower() == "scissor":
if computer.lower() == "lizard":
youWin()
elif computer.lower() == "paper":
youWin()
else:
youLose()
In this case, the .lower() would ensure that whatever the player input to the command prompt, it would always be a lowercase. So if I did PapEr the computer program would read it as paper. or if I did papeR, the computer would see it as paper, and so on.
Ivan Kazakov
Courses Plus Student 43,317 PointsIvan Kazakov
Courses Plus Student 43,317 Pointsit allows you to evaluate the input regardless of the register it was typed in by the user. Otherwise,
Y
would evaluate to false, together withn
andN
and only smally
would evaluate toTrue
in your expression.