03-input-output.py
python-programming / programming-foundations  ·  Lesson 03
Lesson 03 — Programming foundations

Input & Output

Display information and collect text from the user.

print() sends output to the screen. input() displays a prompt, waits for the user, and returns what they typed as a string.

Predict

Read the code first. Do not run it yet.

input-output-predict.py
name = input("Name: ")
print("Hello", name)
Predict before running
  • If the user types Aisha, what will be printed?
  • Which line is input and which line is output?
  • What value is stored in name?

Investigate

Use this code to explain exactly how the program works.

input-output-investigate.py
first = input("First name: ")
surname = input("Surname: ")
print(first, surname)
Investigate the code
  • How many inputs are collected?
  • In what order are the statements executed?
  • What data type does input() return?

Output with print()

output.py
print("Ready")
score = 14
print("Score:", score)
Output
Ready
Score: 14
How it works
  • Text goes inside quotation marks.
  • Variables can be included in print().
  • When items are separated by commas, Python inserts spaces between them.

Input with input()

input.py
city = input("City: ")
print("You entered", city)
Output
City: London
You entered London
Input returns textEven if the user types digits, input() returns a string. Converting input to a number is covered later.

Self-study guide

Input brings data into a program; output sends information out. OCR Exam Reference Language and Python both use input(...) and print(...), so this is one of the closest syntax matches between the two.

The important Python-specific rule is that input() returns text. Even if a user types digits, Python initially receives a string. If you need numeric arithmetic, you must cast the input first.

OCR focusUse input and output practically. Be able to read code containing input(...) and print(...), and remember the Python rule that keyboard input is returned as a string.

OCR Exam Reference Language ↔ Python

OCR Exam Reference Language / termPythonWhat to remember
name = input("Name: ")name = input("Name: ")Same basic form.
print(name)print(name)Same basic form.
Inputinput()The program receives data.
Outputprint()The program communicates a value or message.
Read both formsIn an examination you may see OCR Exam Reference Language even when you have learned the idea in Python. Translate the structure, not just the symbols.

Worked example

This program collects three pieces of text and then produces labelled output.

lesson-03-worked.py
name = input("Name: ")
favourite_game = input("Favourite game: ")
team = input("Team: ")

print("Player:", name)
print("Game:", favourite_game)
print("Team:", team)
Output
Player: Maya
Game: Rocket League
Team: Blue
Work through it line by line
  • Each input() pauses the program and waits for a response.
  • The returned text is stored in a variable.
  • The later print() statements can reuse the stored values.
  • A comma in print() can display several values without manually converting them to one string.

Another example

Numeric-looking input is still text until you cast it.

lesson-03-example-2.py
age_text = input("Age: ")
print(type(age_text))
Output
<class 'str'>
Why this example matters
  • Typing 15 does not make age_text an integer.
  • This is why numeric-input programs commonly use int(input(...)) or float(input(...)).

Common mistakes

Avoid these errors
  • Trying to add a number directly to uncast input() data.
  • Forgetting to store the result of input() in a variable when it is needed later.
  • Producing unclear output with no labels when the user needs to know what a value means.
Exam-safe warningPython input() always returns a string. Cast before numeric calculations.

Exam-ready checklist

You should now be able to
  • Write simple input and output statements.
  • Trace where an input value is stored and later used.
  • Explain why Python input may need casting.
  • Recognise that OCR ERL uses the same input() / print() forms.

What you have learned

  • print() produces output.
  • input() collects user input.
  • The result of input() is a string.
  • Statements normally run from top to bottom in sequence.