Python How to scan for a certain letter in a input
Question:
So, I’m making some code that I want it to do a if statement that looks through the input and if it sees a certain character will then go print a message.
password = input("password: ")
if(password == "scan for a certain letter or something"):
print("Please use don't use these smybols: / ; : , . > < ? ! @ # $ % ^ & * ( ) [ ] { } | ")
it dosen’t have to be a if statement if that is what it turns out to be, I just want to know how to do it.
Answers:
You can use the in
operator with the any()
function:
prohibited = set("/ ; : , . > < ? ! @ # $ % ^ & * ( ) [ ] { } | ]".split())
password = input("password: ")
if any(char in prohibited for char in password):
print(f"Please use don't use these symbols: {prohibited}")
I think it will be more convenient for the user to get an exhaustive list of invalid characters used by him so that he can correct the input:
unacceptable_symbols = r"/;:,.><?!@#$%^&*()[]{}| "
password = r"f;kjad%9203)29e2" # test string
lst = [x for x in password if x in unacceptable_symbols] # makes list of all used unacceptable symbols
if len(lst) > 0:
print("You have used the following unacceptable characters: " + ' '.join(lst))
print("Please don't use these symbols: " + ' '.join(list(unacceptable_symbols)))
# Output:
# You have used the following unacceptable characters: ; % )
# Please don't use these symbols: / ; : , . > < ? ! @ # $ % ^ & * ( ) [ ] { } |
I have tried to implement this code with very simple logic. And its working perfectly.
s="/ ; : , . > < ? ! @ # $ % ^ & * ( ) [ ] { } | "
s1=s.split()
s3=input("enter the string: ")
for i in s3:
if(i in s1):
print("please dont use the given symbol:",i)
So, I’m making some code that I want it to do a if statement that looks through the input and if it sees a certain character will then go print a message.
password = input("password: ")
if(password == "scan for a certain letter or something"):
print("Please use don't use these smybols: / ; : , . > < ? ! @ # $ % ^ & * ( ) [ ] { } | ")
it dosen’t have to be a if statement if that is what it turns out to be, I just want to know how to do it.
You can use the in
operator with the any()
function:
prohibited = set("/ ; : , . > < ? ! @ # $ % ^ & * ( ) [ ] { } | ]".split())
password = input("password: ")
if any(char in prohibited for char in password):
print(f"Please use don't use these symbols: {prohibited}")
I think it will be more convenient for the user to get an exhaustive list of invalid characters used by him so that he can correct the input:
unacceptable_symbols = r"/;:,.><?!@#$%^&*()[]{}| "
password = r"f;kjad%9203)29e2" # test string
lst = [x for x in password if x in unacceptable_symbols] # makes list of all used unacceptable symbols
if len(lst) > 0:
print("You have used the following unacceptable characters: " + ' '.join(lst))
print("Please don't use these symbols: " + ' '.join(list(unacceptable_symbols)))
# Output:
# You have used the following unacceptable characters: ; % )
# Please don't use these symbols: / ; : , . > < ? ! @ # $ % ^ & * ( ) [ ] { } |
I have tried to implement this code with very simple logic. And its working perfectly.
s="/ ; : , . > < ? ! @ # $ % ^ & * ( ) [ ] { } | "
s1=s.split()
s3=input("enter the string: ")
for i in s3:
if(i in s1):
print("please dont use the given symbol:",i)