-1

I have one doubt in the Python Input method.

When I am entering input, it is always considering as String in Python. How to get the Input value in many data types.

As Example:

If I enter Integer value as input then the Code supposed to take that as Integer.

Code:

a=str(input("Enter A Value \n"))

In the above code, it converts my input always as String. Because I used str there.

If I remove str from there and if I type some numbers in the input will it take as an integer?

  • 1
    You need to make a type casting manually. – Dashzeveg Galbadrakh Apr 06 '20 at 03:54
  • 2
    Without `str` there, your input would still be a string. You need to convert it from string to integer if that's what you want. – dspencer Apr 06 '20 at 03:55
  • Does this answer your question? [How can I read inputs as numbers?](https://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-numbers) – dspencer Apr 06 '20 at 03:55
  • 2
    Alternatively, this other SO thread: [Can the input() function in Python dynamically detect the input's data type?](https://stackoverflow.com/questions/57013667/can-the-input-function-in-python-dynamically-detect-the-inputs-data-type) – Oliver.R Apr 06 '20 at 03:57
  • 1
    No, `input` **always returns a `str`**. It is your job to parse that string into some python object, whether that be an `int`, a `float` or a complex data structure. – juanpa.arrivillaga Apr 06 '20 at 04:43

1 Answers1

0

Python 3.x will always consider input as string you have to type cast each field manually.

int(input('Enter Number')) str(input('Enter String'))

Mannya
  • 125
  • 1
  • 2
  • 12