Sololearn: Learn to Code
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5
If you are sure that you can enter the input data properly, there is some code that can handle input elements as different data types. For a practical use to enter different data types, i would prefer to get them as string, and convert them in the next step in a try... except... block. This is the code that can handle such a task: #Created by Amzat: # best solution with flexible conversion during input #types = (int, int, str) # can also use float # Enter 7 9 ABC => [4, 7, 'ABC'] inp = [typ(value) for typ, value in zip((int, int, str), input().split())]
20th Mar 2020, 4:45 PM
Lothar
Lothar - avatar
+ 3
As HonFu pointed out input is always str Python 3 has a built-in function input() to accept user input. But Python 3 doesn’t evaluate the data received with input function, i.e., Python input() function always converts the user input into a string then returns it to the calling program. i.e., the type of user input is still a string. https://pynative.com/python-check-user-input-is-number-or-string/
20th Mar 2020, 4:20 PM
BroFar
BroFar - avatar
+ 1
Input in Python is always str. If you need to convert all or part of it to something else, you need to do it manually afterwards.
20th Mar 2020, 4:16 PM
HonFu
HonFu - avatar
0
Give a sample input. My first thought is to use a regular expression.
20th Mar 2020, 5:38 PM
rodwynnejones
rodwynnejones - avatar