Input and Output
Talking with the person running your program
In this lesson
Programs talk to people through input and output: input() reads what someone types, and print() shows results back. You’ll learn to read input, convert it to the type you need, and present output clearly.
Explain it like I’m 5
input() pauses the program, waits for the person to type something and press Enter, then hands you what they typed, always as text, even if they typed digits.
input() always gives you a string
answer = input("Your name? ") shows the prompt, waits, and stores the typed text in answer. The key fact: the result is always a string. If the person types 25, you get the string "25", not the number, so you can’t do math with it directly.
Converting input to numbers
Wrap the input in int(...) or float(...) when you need a number: age = int(input("Age? ")). If the person types something that isn’t a number, that conversion raises a ValueError, which you’ll learn to handle in the exceptions lesson.
# Picture the user typing 20 at the prompt:
age_text = "20" # this is what input() would return
age = int(age_text)
print(f"Next year you will be {age + 1}.")
Next year you will be 21.
Pretend the user entered "7". Convert it to a number, multiply by 3, and print the result with a label.
entered = "7"
number = int(entered)
print("Triple is", number * 3)
Convert the string with int() before doing math.
entered = "7"
number = int(entered)
print("Triple is", number * 3)
Triple is 21
Formatting output
Combine values with commas in print(), or use f-strings for full control: print(f"Hi {name}, next year you’ll be {age + 1}"). f-strings keep the sentence readable and let you compute inside the braces.
name = "Ada"
age = 20
print("Hi", name + ",", "you are", age)
print(f"Hi {name}, next year you will be {age + 1}.")
Hi Ada, you are 20 Hi Ada, next year you will be 21.
Reading several values from one line
A single input() returns one whole line of text. To read several values at once, split that line into pieces with .split(), which cuts on spaces by default: parts = input().split() gives you a list you can pull apart and convert.
Input lives only for the run of the program, though. When you need data to survive after the program ends, write it to a file instead, learn more about reading and writing files here.
# Pretend the user typed: 12 5
line = "12 5"
width_text, height_text = line.split()
width = int(width_text)
height = int(height_text)
print("Area:", width * height)
Area: 60
Common mistake: Doing math on input() without converting
input() returns a string, so input() + 1 tries to add a number to text and raises a TypeError (or concatenates unexpectedly).
Convert first: n = int(input(...)), then use n.
n = int("5")
print(n + 1) # 6
What type does input() always return?
Always a string, convert it with int() or float() when you need a number.
How do you read a whole number from input named age?
Read the text with input(), then convert it with int().
Mini exercise (easy)
Imagine the user entered "12" for a quantity and the price is 5. Convert the quantity and print the total cost.
There’s no typing box here, so the quantity is given as text, exactly what input() returns. Convert it and print the total cost.
quantity_text = "12" # imagine the user typed this
price = 5
total = 0 # TODO: convert the quantity and multiply by price
print(total)
qty = int("12"), then qty * 5.
quantity_text = "12"
price = 5
total = int(quantity_text) * price
print(total)
60
assert total == 60, "12 items at 5 each should total 60"
print("✓ Correct!")