This question already has answers here:
How do I do a case-insensitive string comparison?
(15 answers)
Closed 2 years ago.
I want to make my user's input (in Python) case insensitive. Like if user type admin, Admin ADMIN or even he or she types like aDMIN. This should be insensitive so i can get exact input
Use lower() : s = input().lower()
Convert your user's input to lower case. Then whatever you compare it to can be lower case.
string = input("Enter something: ")
string = string.lower()
Just use the lower() function, it converts a string to lower case.
myStr = JAMES
myStr.lower() > james
You can just compare it to lower case admin every time.
input_str = input("Enter role: ")
if input_str.lower() == "admin":
pass
Related
This question already has answers here:
How to read keyboard input?
(5 answers)
Closed 11 months ago.
I'm a noob in programming (in python), I want to know how I could read data from user's keyboard in order to make a calculator for example based on the numbers he entered.
Thank you.
This is for sure a duplicate question but you can use this:
a = int(input("Enter the first number"))
b = int(input("Enter the second number"))
... do what you want with a and b.
The input() statement is used for this purpose.
But the input statement accepts string input, so in order to take integral input, you need to convert the input to integers. Code:
a = int(input('Enter a Number'))
This will prompt the user to provide input and then store it in the variable a. You can then use it anywhere. In the same way, if you want to input decimal numbers, you need to convert the type to float:
a = float(input('Enter a Number'))
This question already has answers here:
String contains any character in group?
(5 answers)
Closed last year.
I want to check if username has any Character from blockCharacter but i could'nt do it and this is my code
user = input("Whats your name\n").lower()
user = (user.title())
BlockChar = ["+","none","-"]
if user == BlockChar:
print("That does'nt not feels right. Try Again")
breakpoint
print ("Welcome " + user)
I am new to codeing and stack overflow so i need some help with it
The any function and a generator expression will work quite nicely to determine if any of the strings in BlockChar are in your user string.
if any(ch in user for ch in BlockChar):
...
This question already has answers here:
How do I do a case-insensitive string comparison?
(15 answers)
Closed 5 years ago.
So basically my question is this. Let's say I have a list with values
l1st = ['dog', 'cat', 'bird', 'monkey']
And I ask the user to input
input = raw_input('Please enter the word you wish to search for: ')
Here's the big question. Let's say the person wants to search for
dog
but types it in as
Dog
I don't want the program to give him an error message just because he got the capitalization wrong. What is a method I could use for a list.index search to look for, if not the exact word, the closest similar word?
You can compare the strings by turning both into their lower case form. Here's the documentation. Just as you're doing the search use:
if input.lower() == item.lower():
do_stuff()
You can use the .lower() method to take the input and create the lower case version. You can also look at this link https://docs.python.org/2/library/difflib.html#difflib.get_close_matches which will get closest match to a word froma. List. Hope this helps
This question already has answers here:
Case insensitive 'in'
(11 answers)
Closed 6 years ago.
I'm a little confused regarding a Python exercise that requires a case insensitive input.
This part of the excercise that confuses me:
Make sure your comparison is case insensitive. If 'John' has been used, 'JOHN' should not be accepted.
Here is my code:
current_users = ['username_1', 'username_2', 'username_3', 'username_4',
'username_5']
new_user = ['new_user_1', 'new_user_2', 'new_user_3', 'username_1', 'Username_2']
for username in new_user:
if username in current_users:
print("Please enter a new username.")
elif username not in current_users:
print("This username is available.")
My problem is that I'm trying to get my code to reject "Username_2" because of the capital "U" but I have no idea how to do this. I'm reading through Python Crash Course by Eric Matthes and am currently on chapter 5 but I don't recall being taught how to reject case insensitive inputs yet.
I know of the upper(), lower(), and title() string methods and I tried using:
username.lower() == username.upper()
new_user.lower() == new_user.upper()
before my for loop, but this just results in a syntax error.
You can convert each new username to lower case and compare it to a lower case version of the user list (created using a list comprehension).
lower_case_users = [user.lower() for user in current_users]
if new_user.lower() in lower_case_users:
# Name clash
You may want to store your usernames in lowercase when they are first created to avoid creating a lower case version from the user list.
This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Multiple if statements under one code, with multiple conditons [duplicate]
(3 answers)
Closed 8 years ago.
It is pretty self-explanitory from the code but I want to check if the input is not equal to these values than ask again. I thought this would work but it doesn't and its glitchy, what is a better way to do this?
type=input("Please choose an option: ")
while type.isalpha() == False:
type=input("Please choose an option: ")
while type != ("a" and "A" and "b" and "B" and "c" or "C"):
type=input("Please choose an option: ")
Simply do while not type in ("a","A","b","B" ...) to check whether type is one of the listed elements.
The code above is, as mentioned in comments, equivalent to while type != someListElement because the and and or are evaluated first.
You would need to write:
while (type != "a" and type !="A" and type !="b" and type !="B" and type !="c" or type !="C"):
I think the simplest solution would be to use
type = raw_input("Please choose an option: ")
while type not in list('AaBbCc'):
type = raw_input("Please choose an option: ")
list will convert from a string to a list of single-character strings, which you can then test for inclusion using in. I don't think you need the test for isalpha, because everything you're checking for is already a letter.
Also, you should always use raw_input rather than input to get user input, because raw_input always returns a string, while input tries to eval what the user enters, which is not what you want.
(This is assuming you're using Python 2. If you're using Python 3, input is what raw_input was before, and raw_input no longer exists.)