How To Make Input Not Case Sensitive in Python

How To Make Input Not Case Sensitive in Python | We will discuss how to make input not case sensitive or case insensitive in Python programming languages. So, let us start.

We can use the string.lower() method to make user input strings case-insensitive, e.g. user_input.lower(). Not only lower case but we can also convert the input to upper case.

How To Make User Input Not Case Sensitive In Python using lower()

user_input = input('Do you like Movies(yes/no): ')

option_1 = 'yes'
option_2 = 'no'

if user_input.lower() == option_1.lower():
    print('User typed yes')
elif user_input.lower() == option_2.lower():
    print('User typed no')
else:
    print('Type yes or no')

Output:-

Do you like Movies(yes/no): yes
User typed yes

Do you like Movies(yes/no): YES
User typed yes

Do you like Movies(yes/no): YeS
User typed yes

The input() function is used to take input from the user. We used the string.lower() function to convert both strings to lowercase.

The str.lower() method produces a copy of the string with all the case characters flipped to lowercase.

print('YES'.lower())  # yes

print('YES'.capitalize())  # Yes

print('yes'.upper()) # YES

The process doesn’t change the original string, it only produces a  recent string. We could accomplish the same result by transforming both strings to uppercase.

Python How To Make Input Not Case Sensitive Using upper()

user_input = input('Do you like Movies(yes/no): ')

option_1 = 'yes'
option_2 = 'no'

if user_input.upper() == option_1.upper():
    print('user typed yes')
elif user_input.upper() == option_2.upper():
    print('user typed no')
else:
    print('Type yes or no')

Output:-

Do you like Movies(yes/no): nO
user typed no

Do you like Movies(yes/no): Hello
Type yes or no

Do you like Movies(yes/no): yes
User typed yes

Do you like Movies(yes/no): YES
User typed yes

Do you like Movies(yes/no): YeS
User typed yes

The string.upper() technique produces a copy of the string with all the case characters altered to uppercase.

So, We have discussed how to make input not case sensitive in Python programming language. Hope you all find it useful.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *