Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Case insensitive user input strings

Which function to use for making user input strings case insensitive?

correctAnswer = "London"

userGuess = input("What is the capital of  Great Britain?: ")

if userGuess == "London":
    print("Correct!")
else:
    print("Wrong")

I have tried the following functions after the string:

.lower()
.capitalize()
.casefold()

The output is still 'wrong' although the answer should be correct when input is:
- london
- lOndoN
- loNDON

And so on..

like image 415
vectorizinglife Avatar asked Jun 22 '26 07:06

vectorizinglife


1 Answers

In your string comparison, the correct answer itself had the first letter capitalized.

correctAnswer = "london"

userGuess = input("What is the capital of  Great Britain?: ").lower()

if userGuess == correctAnswer:
    print("Correct!")
else:
    print("Wrong")
like image 99
Vikhyat Agarwal Avatar answered Jun 23 '26 21:06

Vikhyat Agarwal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!