Reading
User Input
Learn how to capture keyboard input from users with input() and convert it to numeric types for calculations.
What Is User Input?
Programs become interactive when they can receive data from the person running them. Instead of hard-coding every value, you ask the user to type something, then your program responds to that data in real time.
The input() Function
Python's built-in input() function pauses the program, displays an optional prompt message, waits for the user to type something and press Enter, then returns what they typed as a string.
How input() Works
Strings vs Numbers
When input() returns "5", Python sees it as text, not a quantity. The + operator on strings means concatenation, not addition:
Type Conversion Functions
Python provides built-in functions to convert strings into numeric types. These are the three you will use most:
The Core Pattern
To read a number from the user, wrap the input() call inside the conversion function you need:
int() vs float()
| int() | float() | |
|---|---|---|
| Accepts "42" | ✓ returns 42 | ✓ returns 42.0 |
| Accepts "3.14" | ✕ ValueError | ✓ returns 3.14 |
| Accepts "hello" | ✕ ValueError | ✕ ValueError |
| Best for | Counts, ages, IDs | Prices, measurements |
How to Apply It
Example 1: Age Calculator
Example 2: Temperature Converter
Example 3: Rectangle Area
Two-Step Conversion
You can also split input and conversion into two separate lines. This can make code easier to read and debug:
Without Conversion
If you forget to convert, Python will not crash immediately, but you will get wrong results or a TypeError:
Common Mistakes
Summary Reference
| Task | Code Pattern | Result Type |
|---|---|---|
| Read text | input("...") | str |
| Read integer | int(input("...")) | int |
| Read decimal | float(input("...")) | float |
| Convert back | str(number) | str |
| Decimal to int | int(float(input("..."))) | int |
Question 1 of 5
What type does input() always return in Python?
Question 2 of 5
What is the output of this code?
x = input() (user types 4)
print(x + x)
Question 3 of 5
Which line correctly reads a decimal number from the user?
Question 4 of 5
What happens when you run int("3.14")?
Question 5 of 5
A student writes this code. What does it print when the user enters 10?
n = input("Number: ") result = n * 2 print(result)