Python for beginners · Lesson 5 of 15

Getting input from the user

Ask the user a question with input(), understand why the answer is always text, and turn it into a number with int() or float().

About 12 minutes · code runs in your browser, nothing to install

Programs become interactive when they ask questions. input() stops the program, waits for the user to type something and press Enter, and gives back what they typed.

On this site, the code boxes come with the answers already typed in for you (look for the input lines next to the code), so every run prints the same result. Try changing them.

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

Typed answer: Ana

A prompt inside input()

You can put the question straight into input(). It is shown on the same line, and the cursor waits right after it. In the output box below, the typed answer itself is not shown, so the prompt and the next printed line run together.

Pythoneditable
city = input("City: ")
print("You typed:", city)
Output
City: You typed: Tokyo

Typed answer: Tokyo

input() always gives you text

Whatever the user types, input() returns a string, even if it looks like a number. Adding two strings glues them together instead of adding them. Convert the answer with int() for whole numbers or float() for decimals.

Pythoneditable
a = input()
b = input()
print(a + b)
print(int(a) + int(b))
Output
53
8

Typed answers: 5 and 3

You can convert in the same line as you ask: int(input()). Python runs the inner call first and then converts what it returned.

Pythoneditable
print("Price of one coffee?")
price = float(input())
print("How many?")
count = int(input())
total = price * count
print(f"Total: {total:.2f}")
Output
Price of one coffee?
How many?
Total: 7.20

Typed answers: 2.40 and 3

Watch out: int("3.5") or int("ten") stops the program with a ValueError, because the text is not a whole number. For now, type sensible answers; in Errors and exceptions you will learn to catch these mistakes.
Pythoneditable
print("What year were you born?")
year = int(input())
age = 2026 - year
print(f"In 2026 you turn {age}.")
Output
What year were you born?
In 2026 you turn 36.

Typed answer: 1990

✎ Exercise

Ask for a temperature in Celsius, convert it to Fahrenheit (multiply by 9, divide by 5, add 32) and print both in one sentence like 20.0 Celsius is 68.0 Fahrenheit. Read the answer with float(input()); the typed answer is 20.

Hint: Use float(input()) so decimals like 21.5 also work.

Your codewrite your answer, then Run
print("Temperature in Celsius?")
# your code here

Expected output:

Temperature in Celsius?
20.0 Celsius is 68.0 Fahrenheit
Show a solution
Solutioneditable
print("Temperature in Celsius?")
celsius = float(input())
fahrenheit = celsius * 9 / 5 + 32
print(f"{celsius} Celsius is {fahrenheit} Fahrenheit")
Output
Temperature in Celsius?
20.0 Celsius is 68.0 Fahrenheit

Check yourself

1. What type does input() return?

2. The user types 4 twice. What does print(input() + input()) show?

3. Which line reads a decimal number like 2.75 correctly?

Spotted a mistake on this page? Tell us