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.

Asked By: Zacky2613

||

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}")

Answered By: Amal K

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: /  ; : , . > < ? ! @ # $ % ^ & * ( ) [ ] { } |  
Answered By: Алексей Р

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)
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.