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.
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.
# Your code hereShow 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 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.