Lesson 13 of 70 – Python Input
19%

Python Input

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.

Note: The input() function returns the value entered by the user as a string. If you need a number, you can convert the input using int() or float().
What is input()?

The input() function waits for the user to enter some data.

name = input()

print(name)

If the user enters:

Rahul
Output:
Rahul
Using input() with a Message

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:

Output:
Amit
Taking Name as Input
name = input("Enter your name: ")

print("Hello", name)

If the user enters Rahul:

Output:
Hello Rahul
Input Always Returns a String

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:

Output:
<class 'str'>
Important: Even though the user enters 20, Python initially treats it as the string "20".
Taking Integer Input

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:

Output:
20
<class 'int'>
Taking Float Input

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:

Output:
99.5
<class 'float'>
Taking Multiple Inputs

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
Output:
Name: Rahul
City: Patna
Taking Two Numbers
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))

print(num1)
print(num2)

If the user enters 10 and 20:

Output:
10
20
Adding Two User Inputs
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:

Output:
Sum: 30
Subtraction Using Input
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:

Output:
Difference: 30
Multiplication Using Input
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:

Output:
Product: 30
Division Using Input
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:

Output:
Result: 4.0
Taking Marks as Input
marks = float(input("Enter your marks: "))

print("Your marks are:", marks)

If the user enters 85.5:

Output:
Your marks are: 85.5
Taking Age as Input
age = int(input("Enter your age: "))

print("You are", age, "years old.")

If the user enters 21:

Output:
You are 21 years old.
Taking Boolean Input

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:

Output:
True
Input and String Concatenation

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
Output:
My name is Rahul
I live in Patna
Input with f-Strings

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
Output:
My name is Rahul and I am 20 years old.
Invalid Numeric Input

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.

Important: Later, you will learn how to handle such errors using Exception Handling.
Practical Example
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
Output:
----- Student Details -----
Name: Rahul
Age: 20
Marks: 85.5
Key Points
  • The input() function is used to take data from the user.
  • By default, input() returns a string.
  • Use int() to take integer input.
  • Use float() to take decimal input.
  • Multiple inputs can be taken using multiple input() statements.
  • User input can be used in calculations.
  • Invalid numeric input can cause a ValueError.
  • Input makes Python programs interactive.

🧠 Quick Quiz

Question: What data type does the input() function return by default?