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

What's the default variable type for input in Python 3.x?

I’ve been trying to write a simple checking script for whatever the user inputs.

The script, however, automatically assumes that anything being input is a string by default. Is there a way to change that without using int(input) or float(input)? The point of the script is to use the isinstance to get the correct output.

l = input("Input: ")
if isinstance(l, int) and l >= 0:
    print ("l is %d and is more/equal to zero" % l)
elif isinstance(l, int) and l < 0:
    print ("l is %d and is lesser than zero" % l)
elif isinstance(l, float): 
    print ("l is %.2f and is a float variable" % l) 
elif isinstance(l, str):
    print ("l is %s and is a string" % l)

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

>Solution :

The input() function always returns a string. If you want it to be some other type, you’ll need to manually convert it.

my_str = input("Enter a number")
my_int = int(my_str)
my_float = float(my_str)
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