Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading