How to check a data type in python?

I am trying to check a data type in python.
I want to use an if statement to check if the data is a string or not. And everytime i input an integer it returns the value as a string.

Here is the code.

inp = input("Input: ")

if type(inp) != str:
    print("You must input a string")
else:
    print("Input is a string")

this is the message it returns.

Input is a string

I want it to print "You must input a string"

>Solution :

Brother, First of all numbers can also be strings. Strings are anything which is enclosed in double quotes. Anyway if all you need is to get an input and verify that it’s not a number you can use:

inp = input("Input: ")

if inp.isdigit():
    print("You must input a string")
else:
    print("Input is a string")

Or if you wish to have a string with no digits in it the condition will go something like this:

inp = input("Input: ")

if any(char.isdigit() for char in inp) :
    print("You must input a string")
else:
    print("Input is a string")

Have a good day

Leave a Reply