-1

In python2 the input() function does not convert the input to string as it is done in python3.

For eg in python2 if input is :- [[1, 2, 3], [4, 5, 6]] then this returns a 2d List

But in python3 the input() function converts the input to string

Is there any similar function in python3 which prevents the typecasting of input to string ?

deepakgupta191199
  • 370
  • 1
  • 2
  • 10

1 Answers1

1

Use eval(input()) as mentioned here

def InputList():
    i = eval(input("What's your input?\n"))
    print(type(i))

InputList()

Output:

What's your input?
[0, 1]
<class 'list'>

Or as @Olvin Roght mentioned, ast.literal_eval() is a bit more complicated but more secure way.

import ast
def InputList():
    i = input("What's your input?\n")
    i = ast.literal_eval(i)
    print(type(i))

InputList()

This will output the same as above

Kiwirafe
  • 375
  • 2
  • 9