01 / 20
>>>

Reading
User Input

Learn how to capture keyboard input from users with input() and convert it to numeric types for calculations.

Python

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.

name = input("What is your name? ") print("Hello,", name)

How input() Works

Prompt shown "Enter age: " User types 17 + Enter Returns string "17" Convert with int() "17" becomes 17 Use in math
\( 17 + 1 = 18 \)
Step 1 Step 2 Step 3 Step 4 Step 5

Strings vs Numbers

When input() returns "5", Python sees it as text, not a quantity. The + operator on strings means concatenation, not addition:

# String concatenation (not math!) "5" + "3" # Result: "53" # Numeric addition (what we want) 5 + 3 # Result: 8
"5" + "3"
Joins text: produces "53"
5 + 3
Adds numbers: produces 8

Type Conversion Functions

Python provides built-in functions to convert strings into numeric types. These are the three you will use most:

int()
Converts to a whole number (integer). Example: int("42") gives 42
float()
Converts to a decimal number. Example: float("3.14") gives 3.14
str()
Converts back to text. Example: str(100) gives "100"

The Core Pattern

To read a number from the user, wrap the input() call inside the conversion function you need:

# Read an integer age = int(input("Enter your age: ")) # Read a float (decimal) price = float(input("Enter the price: "))

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

1
Decide what data you need. What question will you ask the user?
2
Choose the right type. Is it a whole number (int), decimal (float), or text (str)?
3
Write the input call. Wrap input() inside the conversion function.
4
Store in a variable. Assign the result to a descriptive variable name.
5
Use the variable. Now you can do math, comparisons, or display it.

Example 1: Age Calculator

birth_year = int(input("Enter your birth year: ")) age = 2025 - birth_year print("You are about", age, "years old.")

Example 2: Temperature Converter

fahrenheit = float(input("Temperature in F: ")) celsius = (fahrenheit - 32) * 5 / 9 print("That is", round(celsius, 1), "degrees C.")

Example 3: Rectangle Area

length = float(input("Enter length: ")) width = float(input("Enter width: ")) area = length * width perimeter = 2 * (length + width) print("Area:", area) print("Perimeter:", perimeter)

Two-Step Conversion

You can also split input and conversion into two separate lines. This can make code easier to read and debug:

# Two-step approach text = input("Enter a number: ") # Step 1: get string num = int(text) # Step 2: convert # One-step approach (same result) num = int(input("Enter a number: "))

Without Conversion

If you forget to convert, Python will not crash immediately, but you will get wrong results or a TypeError:

x = input("Enter a number: ") # x is "5" (string) print(x * 3) # Prints "555" not 15! print(x + 10) # TypeError: can't add str + int
"5" * 3
String repetition: "555" (the string repeated 3 times)
"5" + 10
TypeError because Python cannot add a string and an integer

Common Mistakes

✕ Forgetting to convert
Writing x = input(...) and then doing math on a string. Always wrap with int() or float().
✕ Using int() on decimals
int("3.14") raises a ValueError. Use float() for decimals, or int(float(...)) to truncate.
✕ Missing the prompt string
Calling input() with no argument works, but the user sees a blank cursor and does not know what to type.

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?