The input() function in Python is used to take information from the user while a program is running. It allows programs to become interactive by accepting data such as name, age, address, marks, and other values.
The input() function waits for the user to enter some data.
name = input()
print(name)
If the user enters:
Rahul
You can display a message inside the input() function to tell the user what information should be entered.
name = input("Enter your name: ")
print(name)
The program displays:
Enter your name:
If the user enters Amit:
name = input("Enter your name: ")
print("Hello", name)
If the user enters Rahul:
By default, the input() function returns the entered value as a string.
age = input("Enter your age: ")
print(type(age))
If the user enters 20:
To take an integer from the user, use int() with input().
age = int(input("Enter your age: "))
print(age)
print(type(age))
If the user enters 20:
To take a decimal number from the user, use float().
price = float(input("Enter price: "))
print(price)
print(type(price))
If the user enters 99.50:
You can take input from the user multiple times.
name = input("Enter your name: ")
city = input("Enter your city: ")
print("Name:", name)
print("City:", city)
If the user enters:
Rahul
Patna
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
print(num1)
print(num2)
If the user enters 10 and 20:
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
sum = num1 + num2
print("Sum:", sum)
If the user enters 10 and 20:
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
result = num1 - num2
print("Difference:", result)
If the user enters 50 and 20:
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
result = num1 * num2
print("Product:", result)
If the user enters 5 and 6:
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
result = num1 / num2
print("Result:", result)
If the user enters 20 and 5:
marks = float(input("Enter your marks: "))
print("Your marks are:", marks)
If the user enters 85.5:
age = int(input("Enter your age: "))
print("You are", age, "years old.")
If the user enters 21:
Boolean values can also be created from input, but remember that bool() converts an empty string to False and a non-empty string to True.
value = input("Enter something: ")
print(bool(value))
If the user enters Hello:
Input values can be combined with other strings.
name = input("Enter your name: ")
city = input("Enter your city: ")
print("My name is " + name)
print("I live in " + city)
If the user enters:
Rahul
Patna
Input values can also be displayed easily using f-strings.
name = input("Enter your name: ")
age = int(input("Enter your age: "))
print(f"My name is {name} and I am {age} years old.")
If the user enters:
Rahul
20
If you use int() and the user enters something that is not a valid integer, Python will generate a ValueError.
age = int(input("Enter your age: "))
If the user enters:
twenty
Python cannot convert "twenty" into an integer and an error occurs.
name = input("Enter student name: ")
age = int(input("Enter student age: "))
marks = float(input("Enter student marks: "))
print("----- Student Details -----")
print("Name:", name)
print("Age:", age)
print("Marks:", marks)
Example input:
Enter student name: Rahul
Enter student age: 20
Enter student marks: 85.5
Question: What data type does the input() function return by default?