So far your programs have known everything in advance. input() changes that:
it prints a prompt, waits for a line to be typed, and hands that line back to
you.
The playgrounds on this page really do wait for you. Run the next one, then type an answer into the output area and press Enter.
name = input("What is your name? ")
print(f"Hello, {name}!")Pre-filling answers
A playground can also be given answers in advance, so you can press Run and
watch the whole conversation happen. That is what the stdin setting does on
the examples further down this page.
input() always returns a string
This is the single most common beginner surprise. Whatever the user types,
input() gives you text — even when the text looks like a number.
age = input("Age: ")
print(type(age))
print(age + 10)TypeError: can only concatenate str (not "int") to str. Python is refusing
to guess whether you meant arithmetic or gluing text together. Convert first:
age = int(input("Age: "))
print(type(age))
print(f"In 10 years you will be {age + 10}")The conversion functions
| Function | Turns a value into | Example |
|---|---|---|
int(x) | whole number | int("42") → 42 |
float(x) | decimal | float("3.5") → 3.5 |
str(x) | text | str(42) → "42" |
bool(x) | True/False | bool("") → False |
print(int("42") + 1)
print(float("3.5") * 2)
print("Total: " + str(99))
# int() truncates a float towards zero - it does not round.
print(int(3.9), int(-3.9))
print(round(3.9))
# Text that is not a number raises an error, on purpose.
print(int("forty-two"))That last line fails with ValueError: invalid literal for int() with base 10.
Good — a program that silently turned bad input into 0 would be far worse.
On Day 5 you will learn to catch that error and ask again.
Several inputs
Each call to input() reads one line.
name = input("Name: ")
birth_year = int(input("Birth year: "))
age = 2026 - birth_year
print(f"{name} would be about {age} this year.")Python in the browser
input() here works exactly as it does in a terminal, but a program that is
waiting for input holds the shared Python runtime — other playgrounds on the
page will show "Queued…" until it finishes. Answer it, or press Stop.
Before you write your own
Predict. Every value here has been converted. Decide each line first.
print(int("5") + 3)
print("5" + str(3))
print(float("2.5") * 2)
print(int(7.9))Check your prediction
8
53
5.0
7The second line is the one to check: str(3) makes text, so + joins two
strings. And int(7.9) is 7, not 8 — int cuts off the decimals, it
doesn't round.
Fix. This should print the cost of three tickets at the price typed in —
Total: 36 for a $12 ticket. The 12 is pre-filled. It doesn't crash, which
makes it worse: it prints something plausible-looking and wrong.
price = input("Ticket price: ")
print("Total:", price * 3)Check your fix
It printed Total: 121212, because price is the text "12", and text
times three is the text repeated three times. Convert as you read it:
price = int(input("Ticket price: "))
print("Total:", price * 3)Now it prints Total: 36. A crash would have told you something was wrong; this
kind of bug only shows up if you check the output against what you expected —
which is why it's worth knowing the answer before you run.
Unit converter
Ask for a temperature in Celsius, then print it in Fahrenheit rounded to one
decimal place. The formula is F = C * 9 / 5 + 32.
The answer 21.5 is pre-filled, so pressing Run is enough — but try typing
your own value too. With 21.5, the result your program prints should be:
21.5°C is 70.7°F# Your code hereHint 1: the idea
Three steps: read a line, turn it into a number, apply the formula. input()
always gives text, so the middle step matters — and temperatures can have
decimals, which decides which conversion you want.
Hint 2: a first step
Read and convert in one line, then print what you got before doing any maths:
celsius = float(input("Temperature in Celsius: ")). Once that shows 21.5,
add the formula, and use :.1f in an f-string for the one decimal place.
Show one solution
celsius = float(input("Temperature in Celsius: "))
fahrenheit = celsius * 9 / 5 + 32
print(f"{celsius}°C is {fahrenheit:.1f}°F")float, not int — temperatures are measured, not counted, and int("21.5")
would fail anyway.
Day 1 checkpoint: combine today's ideas
Close the worked examples and try this from memory. It takes about 15–20 minutes.
A study receipt
Use the supplied text values to print a cleaned name, numeric study minutes, and elapsed minutes including breaks. There is one break between sessions, so three sessions have two breaks. Assume sessions is at least one; input validation comes later. Keep calculations numeric until formatting the result. Your program should print exactly:
Ada: 3 sessions
Study: 60 min
Elapsed: 70 minWhen your output matches, try these changes. Each says what to change and what you should then see:
| Change | What you should see |
|---|---|
sessions_text = "1" | Study and Elapsed are both 20: one session has no break |
minutes_text = "30" | Study 90, Elapsed 100: both come from the variables |
name_text = " Grace " | Only the first line changes, to Grace: 3 sessions |
name_text = " Ada "
sessions_text = "3"
minutes_text = "20"
break_minutes = 5
# Clean, convert, calculate, then format the receipt.Hint 1: the idea
Name cleaning and numeric conversion are separate from formatting. Count the gaps between sessions rather than adding a break after the last session.
Hint 2: a first step
Convert the two numeric strings and calculate study time first. Then calculate break time from one fewer than the session count.
Show one solution
name_text = " Ada "
sessions_text = "3"
minutes_text = "20"
break_minutes = 5
name = name_text.strip()
sessions = int(sessions_text)
minutes = int(minutes_text)
study = sessions * minutes
elapsed = study + (sessions - 1) * break_minutes
print(f"{name}: {sessions} sessions")
print(f"Study: {study} min")
print(f"Elapsed: {elapsed} min")Ada: 3 sessions
Study: 60 min
Elapsed: 70 minClose the solution and change one input. Explain which case would expose the mistake you were most likely to make. Begin your next session by rebuilding one small part without looking back.
Day 1 is done
You can now write a program that takes information in, computes something, and reports the result — which is, at bottom, what every program does.
Tomorrow you give your code the two abilities it is still missing: making decisions, and repeating itself.
What you learned
input(prompt)reads one line from the user.- It always returns a string — convert with
int()orfloat()before doing maths. str()converts the other way;int()truncates rather than rounds.- Converting nonsense raises
ValueError, which is a feature, not a nuisance.