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.
name = input("Name: ")
print("Hello", name)- 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.
first = input("First name: ")
surname = input("Surname: ")
print(first, surname)- How many inputs are collected?
- In what order are the statements executed?
- What data type does
input()return?
Output with print()
print("Ready")
score = 14
print("Score:", score)Ready Score: 14
- 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()
city = input("City: ")
print("You entered", city)City: London You entered London
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.
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 / term | Python | What to remember |
|---|---|---|
name = input("Name: ") | name = input("Name: ") | Same basic form. |
print(name) | print(name) | Same basic form. |
Input | input() | The program receives data. |
Output | print() | The program communicates a value or message. |
Worked example
This program collects three pieces of text and then produces labelled output.
name = input("Name: ")
favourite_game = input("Favourite game: ")
team = input("Team: ")
print("Player:", name)
print("Game:", favourite_game)
print("Team:", team)Player: Maya Game: Rocket League Team: Blue
- 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.
age_text = input("Age: ")
print(type(age_text))<class 'str'>
- Typing 15 does not make
age_textan integer. - This is why numeric-input programs commonly use
int(input(...))orfloat(input(...)).
Common mistakes
- 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.
input() always returns a string. Cast before numeric calculations.Exam-ready checklist
- 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.