Python Python Basics All Together Now Gather Information

Lukas Johmann
Lukas Johmann
911 Points

Purpose 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":

Ivan Kazakov
Ivan Kazakov
43,310 Points

it allows you to evaluate the input regardless of the register it was typed in by the user. Otherwise, Y would evaluate to false, together with n and N and only small y would evaluate to True in your expression.

2 Answers

Ivan Kazakov
Ivan Kazakov
43,310 Points

Braces 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
Travis Whiteman
3,342 Points

An 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.