Python Forum
Getting type from input() function in Python 3.0 - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Getting type from input() function in Python 3.0 (/thread-19986.html)



Getting type from input() function in Python 3.0 - leodavinci1990 - Jul-23-2019

If I want to enter a value of a certain data type other than string class <str> into a an input() function. How can this be done. In Python 2.0, there were two input functions, input() and raw_input. input() return whatever value was entered and raw_input() converted all inputs to str. input() & raw_input were combined into input() in Python 3.0

How can I get the same functionality of Python 2.0's input() function?
For example: display data type of any entered value

data = input("Enter any value:")
print(data, " is of type: ", type(data))
I want to enter 1 and return a data type class <int> for example or the same with list, float, dict


RE: Getting type from input() function in Python 3.0 - scidam - Jul-23-2019

(Jul-23-2019, 02:48 AM)leodavinci1990 Wrote: I want to enter 1 and return a data type class <int> for example or the same with list, float, dict

It is hard to tell a computer what you mean when typing 1. It may be a char (in case of python -- a string of len 1),
integer value (as you want), and float value too (1 belongs to the set of real number); or even something else, we can
imagine that it is a complex number 1+i0. However, input is used to enter some characters from keyboard, so it looks natural that input returns a string.

However, you can override its default behavior, e.g.

_input = input

def input(q):
    x = _input(q)
    # if x consist only of digits, it is likely should be integer,
    # lets convert it to integer and return it
    # if x consist of digits and has e, E, +, -, or dot, that might be a float (this should be clarified)
    # lets convert it to float and return it
    # and so on...
    
    # if no condition were satisfied return result as is.
    return x



RE: Getting type from input() function in Python 3.0 - snippsat - Jul-23-2019

(Jul-23-2019, 02:48 AM)leodavinci1990 Wrote: How can I get the same functionality of Python 2.0's input() function?
Use int() or float() like this.
data = int(input("Enter any value: "))
print(f'{data} is of type: {type(data)}')
Output:
Enter any value: 5 5 is of type: <class 'int'>
input() in Python 2 should no be used because it had eval(),so i lot posts was made that should never use input() in Python 2.
Python 3 input() just string,but can use int() and float() as showed.


RE: Getting type from input() function in Python 3.0 - ichabod801 - Jul-23-2019

To get to the point people are avoiding. eval(input(prompt)) in Python 3 is equivalent to input(prompt) in Python 2. But the eval part is exactly why it was changed. The eval function can be used to inject malicious code, much like an SQL injection attack. If this is for code where you are the only user, that's not a problem. But if there is a possibility that an external user could be running the code it becomes a security risk.


RE: Getting type from input() function in Python 3.0 - ndc85430 - Jul-26-2019

I think if you're having to resort to eval, there's likely a better design that can avoid the dangers of it.


RE: Getting type from input() function in Python 3.0 - avorane - Jul-29-2019

Hello,

A little script for evaluate:

import sys
def is_integer(value):
    try:
        tmp = int(value)
        if len(str(tmp)) == len(value):
            return tmp
        else:
            return None
    except:
        return None

def is_float(value):
    try:
        return float(value)
    except:
        return None

def is_string(value):
    return True

conversion = [('integer', is_integer), ('float', is_float), ('string', is_string)]
value = input('Saisir valeur: ')
for type_value, function_evaluate in conversion:
    if function_evaluate(value) is not None:
        print('Type of {} is {}'.format(value, type_value))
        break
Nicolas TATARENKO


RE: Getting type from input() function in Python 3.0 - ichabod801 - Jul-29-2019

I'm not sure why you are checking the length on line five, they will not necessarily be the same. For example, both '100_000' and '5 ' will convert to integers, but when converted back to string the strings will be different lengths.


RE: Getting type from input() function in Python 3.0 - avorane - Jul-29-2019

Hello,

Sorry. Yes, we don't need evaluate the length. It's a bad memory :) In my memory if you evaluate for example '2.5' with int() it returns 2. But no. The length evaluate was in this case.

Nicolas TATARENKO