Python - How to Make User Input Not Case Sensitive

Case insensitive user input strings

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:

How do I make the input case-insensitive?

Use .lower() or .upper() or [.lower() and .capatalize()] according to how you store your months in the list.

How to make user input not case sensitive in Python?

You can convert the string to lower or upper case before doing the comparison.

if action.lower() == "throw it back into the ocean"

What is the proper way to allow a capitalized "Yes" input and a non-capitalized "yes" input or value in Python?

Try something like this:

x = input('...')
if(x == 'Yes' or x == 'yes'):

How to make user-input for Symbols case insensitive

If you are wanting your input to be recast to lowercase so the derivatives that you hard coded always work you could either convert the input to lower case or, more safely, provide locals that remap the uppercase symbols of interest to lower case symbols. For clarity in the following I use S to sympify rather than the parser:

>>> S('x', {'x':'upper'})  # example showing you can replace 'x' with 'upper'
>>> S('X', dict([(str(i), str(i).lower()) for i in symbols('X:Z')]))

In your code you will have to update your locals with the dict() that is being used in my example: loc = locals(); loc.update(dict([(str(i), str(i).lower()) for i in symbols('X:Z')])) and use loc instead of locals() in your code.

Related Topics

Leave a reply