Taking Input in Python

input () function first takes the input from the user and converts it into a string. The type of the returned object always will be <class ‘str’>. It does not evaluate the expression it just returns the complete statement as String.

# Python program showing 
# a use of input()

val = input("Enter your value: ")
print(val)

Type casting for int and float

num = int(input("Enter a number: "))
print(num, " ", type(num))

          
floatNum = float(input("Enter a decimal number: "))
print(floatNum, " ", type(floatNum))

Taking multiple inputs from user in Python

# taking two inputs at a time
x, y, z = input("Values: ").split()
print(x)
print(y)
print(z)

How it Works:

  • input() takes the full input as a single string.
  • .split() divides the string into separate components based on whitespace by default.
  • The values are assigned to individual variables (x, y, z).

Eamples

# Asking for multiple space-separated values
inputs = [i for i in input().split()]

print(inputs)

# taking multiple inputs at a time separated by comma
x = [int(x) for x in input().split(",")]
print(x)

Using map() for Multiple Integer Inputs

If you need to collect multiple inputs in a single line and convert them into integers (or another data type), the map() function is useful. The map() function applies a specified function to each item in an iterable.

# Take space-separated inputs and convert them to integers
a = map(int, input().split())

# Convert the map object to a list and print it
b = list(a)
print(b)