Search code examples
pythonif-statementspecial-characterscontrol-flowalphabet

Check whether a character is an alphabet, digit or special character- PYTHON


Ques - Check whether a character is an alphabet, digit or special character

I have written a python code solution and it is working well. But when I input ("@"), I get the output as an alphabet instead of special character. Can someone tell me why and how to solve it for @.

inp = input("Enter anything : ")
if inp >= "a" and inp <= "z" or inp >= "A" and inp <= "Z":
   print("input is alphabet")
elif inp>="0":
   print("input is number")
else:
   print("special character")

Solution

  • Just add a special catagory for special char

    import re
    
    inp = input("Enter anything : ")
    if inp >= "a" and inp <= "z" or inp >= "A" and inp <= "Z":
       print("input is alphabet")
    elif inp>="0" and inp<="9":
       print("input is number")
    
    special_characters = "!@#$%^&*()-+?_=,<>/"
    if (any(c in special_characters for c in inp)):
       print("special character")