Input and Output

Talking with the person running your program

Beginner 8 min

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 maths 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.

Example
# 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}.")
Output
Next year you will be 21.
Simulating input, then converting and using it.

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)

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.

Example
name = "Ada"
age = 20
print("Hi", name + ",", "you are", age)
print(f"Hi {name}, next year you will be {age + 1}.")
Output
Hi Ada, you are 20
Hi Ada, next year you will be 21.
Comma-separated print versus a tidy f-string.

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.

Example
# 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)
Output
Area: 60
Split one line into two values, then convert. (input() simulated.)

Common mistake: Doing maths on input() without converting

Why it happens:

input() returns a string, so input() + 1 tries to add a number to text and raises a TypeError (or concatenates unexpectedly).

How to fix it:

Convert first: n = int(input(...)), then use n.

n = int("5")
print(n + 1)  # 6

What type does input() always return?

How do you read a whole number from input named age?

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)

What to learn next

You learned to read input() (always a string), convert it to the type you actually need, and format your output. You applied it by turning a typed-in quantity into a number and multiplying it by a price.

You can now write programs that do real work, which makes this the right moment to keep them readable, for others and for future you. Comments and Readability covers when comments help versus add noise, choosing good names, and the basics of PEP 8 style. When you’re ready, continue to the next lesson.