Python Foundations Lesson 5 of 7

Python input() and type conversion

20 min, 5 exercisesOCR 2.2.1, 2.2.2AQA 3.2.7, 3.2.8Edexcel 6.4.1

Builds on lessons 1 to 4. Start at lesson 1

Learn

Reading input as text

input() pauses the program, waits for the user to type a line, and hands that line back to you.

name = input()
print("Hi " + str(name) + "!")
name ← USERINPUT
OUTPUT "Hi " + INT_TO_STRING(name) + "!"
name = input()
print(f"Hi {name}!")

You can give it a prompt to show first, like input("Your name: "). In this course the exercises feed input automatically (you will see it in the stdin box), so we mostly call input() with no prompt to keep the output clean.

Now the trap that catches literally everyone: input() always gives you a string. Even if the user types 5, you get the text "5", not the number 5.

a = input()
b = input()
print(a + b)
a ← USERINPUT
b ← USERINPUT
OUTPUT a + b
a = input()     # user types 5
b = input()     # user types 5
print(a + b)    # 55   string + string glues them together

Learn

Converting text to numbers

To do maths, convert the string first:

  • int("5") gives the int 5.
  • float("2.5") gives the float 2.5.
  • str(5) goes the other way and gives the string "5".

Usually you convert straight away, in the same line:

age = int(input())
age ← STRING_TO_INT(USERINPUT)
age = int(input())

If the text cannot be converted, Python stops with a ValueError. int("hello") fails, and so does int("2.5"), because int() wants a whole number written as digits. (int(2.5) with an actual float is fine though; it chops off the decimal part and gives 2.)

You can check what type something is with type(): type("5") is str, type(5) is int. When a program does something weird with numbers, printing the type is a great first step.

GCSE exam boards
  • OCR 2.2.1 Programming fundamentals, 2.2.2 Data types and casting
  • AQA 3.2.7 Input and output, 3.2.8 Strings
  • Edexcel 6.4.1 User input

Try

Run it: a name and age

The example is already in the editor. Run it, change one thing, and run it again. It reads from the Input box under the editor.

example.py to run
name = input("What's your name? ")
age = int(input("How old are you? "))
print()
print(f"Hi {name}! In 5 years you will be {age + 5}.")

raw = "5"
print(raw + raw, "vs", int(raw) + int(raw))
print(type(raw), type(int(raw)))

Practice

Add two numbers

The program reads two whole numbers, one per line. Print their sum. With the input 7 and 5, the output should be 12.

Your program reads the Input box under the editor.

exercise_1.py to run
a = input()
b = input()
print(a + b)

Practice

Ticket price

Read a number of tickets (a whole number) and a price per ticket (which can have decimals), each on its own line. Print the total with an f-string so the output is exactly Total: 37.50 for 3 tickets at 12.5 each. Show exactly two decimal places.

Your program reads the Input box under the editor.

exercise_2.py to run
tickets = input()
price = input()
print(f"Total: {tickets}")

Break/fixFix the bug

Crash on a price with pence

The program reads a price and a quantity and prints the total. With the price 2.50 it crashes with a ValueError. Fix it so the output is Total: 10.00.

Your program reads the Input box under the editor.

exercise_3.py to run
price = int(input())
qty = int(input())
print(f"Total: {price * qty:.2f}")

PracticePredict the output

Same input, two answers

The input box holds 4. Type exactly what the program prints.

exercise_4.py
n = input()
print(n * 3)
print(int(n) * 3)

Your turn

Minutes on repeat

Read a song's length in seconds (a whole number), then how many times you played it (a whole number). Print how many minutes you spent listening, with one decimal place, like Listened: 10.0 minutes.

Milestones

  1. 200 seconds, 3 plays
  2. 199 seconds, 5 plays
  3. One short play

On your own. Hints open after your first check.

Your program reads the Input box under the editor. Check also tries 2 other inputs.

exercise_5.py to run
Python 3.13

Code editor. Press Control or Command plus Enter to run the code. Tab indents; to move focus out of the editor, press Escape and then Tab.