N3/N4  ·  SDD  ·  Software Design & Development

Input, Output and String Basics

National 3 & National 4 Computing Science Lesson SDD2 of 10 Approx 55 min
Learning intentions
  • Understand how to get input from the user with input()
  • Know that input() always returns a string, and how to convert it
  • Combine text and variables in output using print()
Success criteria
  • I can use input() to store a user's response in a variable
  • I can convert user input to int or float when needed
  • I can produce combined text and variable output with print()
Warm up — what do you already know?

Answer before the lesson begins. These check prior knowledge — it's fine if you're unsure.

1. When you type your name into a website form, what is it usually treated as?

2. Which of these would need to be converted before doing maths on it?

3. True or false: to a Python program, the text "5" and the number 5 are exactly the same thing.

Key vocabulary

input()
A built-in function that pauses the program, waits for the user to type something, and returns it — always as a string.
print()
A built-in function that displays text or values on the screen.
Concatenation
Joining pieces of text together, usually with the + operator.
Type conversion (casting)
Changing a value from one data type to another, e.g. int("5") converts the string "5" to the integer 5.
f-string
A shortcut for combining text and variables in print(), written as f"..." with variables inside { }.
len()
A built-in function that returns how many characters are in a string.

Input, Output and String Basics

Getting input from the user

The input() function pauses a program and waits for the user to type something, then stores whatever they typed. You can display a message at the same time by putting text inside the brackets: name = input("What is your name? ") shows the prompt, waits, and stores the reply in name.

The single most important rule about input() is this: it always returns a string, even if the user types digits. If your program asks "How old are you?" and the user types 13, Python stores it as the string "13", not the integer 13. This catches out almost everyone starting Python.

Converting input to a number

If you need to do arithmetic with something the user typed, you must convert it first, using int() for whole numbers or float() for decimals: age = int(input("How old are you? ")) converts the typed text straight into an integer in one line. If you try to add 1 to an unconverted input() result, Python raises a TypeError, because you cannot do arithmetic on a string.

Combining text and variables in output

There are three common ways to combine text and variables in a print() statement. Concatenation joins strings with +, but every value must already be a string — print("Age: " + str(age)) requires wrapping age in str() first. The comma method is more forgiving: print("Age:", age) automatically converts each value to a string and adds a space between them. The most modern approach is an f-string: print(f"Age: {age}") lets you put a variable directly inside curly brackets within the text, with no conversion needed at all.

Useful string operations

len() tells you how many characters are in a string, e.g. len("Hello") returns 5. String methods like .upper() and .lower() change the case of text: name.upper() converts name to all capital letters without changing the original variable, unless you store the result back into it.

Worked examples

Example 1 — basic input and conversion

A program needs the user's name and age.

1
name = input("What is your name? ") — stored as a string, no conversion needed.
2
age = int(input("How old are you? ")) — converted straight to an integer in one line.
3
Now age can be used in a calculation, e.g. age + 1, without an error.
Example 2 — combining output three ways

Displaying a greeting using the name and age variables from Example 1.

1
Concatenation: print("Hello " + name + ", you are " + str(age) + ".")
2
Comma method: print("Hello", name, "you are", age)
3
f-string: print(f"Hello {name}, you are {age}.") — usually the clearest to read and write.
Example 3 — a simple string operation

A program checks how long a password someone types is.

1
password = input("Enter a password: ")
2
length = len(password) stores how many characters were typed.
3
print(f"Your password is {length} characters long.")
Now you try

A quiz program contains this line: score = input("Enter your score out of 10: "), followed later by total = score + 5.

Answer the following:

  1. What data type is score after the first line runs?
  2. What will happen when the second line, total = score + 5, tries to run?
  3. Rewrite the first line so the program works correctly.
  1. score is a string, because input() always returns a string.
  2. Python will raise a TypeError, because you cannot add a string and an integer together with +.
  3. score = int(input("Enter your score out of 10: ")) — converting straight to an integer fixes the problem.
Common mistakes
Forgetting to convert input(). Trying to use the result of input() directly in a calculation causes a TypeError, because it is a string.
Concatenating a string and a number directly. print("Score: " + score) fails if score is an integer — convert it with str(score) first, or use commas / an f-string instead.
No prompt message inside input(). Writing name = input() with nothing in the brackets leaves the user unsure what to type — always include a clear message.
Confusing commas and + in print(). Commas add a space automatically and accept mixed data types; + adds nothing extra and requires everything to already be a string. Mixing up which one you're using causes either spacing errors or a TypeError.
Tip

N4 pupils: when explaining your code, be specific about why a conversion was needed — "I used int() here because input() always returns a string, and I needed a whole number to do the calculation" is a much stronger explanation than just "I converted it."

Task Set

Questions 1–5 are auto-checked. Questions 6–7 are self-marked — write your answer, then reveal the model answer to check your work. Questions 8–9 are practical tasks in IDLE.

1. What data type does input() always return? TYPE 1

2. Which line correctly stores a whole number typed by the user? TYPE 1

3. What does len("Computing") return? TYPE 1

4. Which of these will run without an error, given score = 8? TYPE 1

5. What will print(f"Total: {5+3}") display? TYPE 1

6. Explain why input() always returning a string can cause a problem, and how a programmer fixes it. TYPE 2 N4

Because input() always returns a string, a program cannot do arithmetic on the result directly, even if the user types digits — trying to would cause a TypeError. A programmer fixes this by converting the result using int() or float(), for example age = int(input("Age: ")), so the value can be used in calculations.

7. Compare concatenation (+) and f-strings as ways of combining text and variables. Which would you recommend, and why? TYPE 2 N3

Concatenation with + requires every value to already be a string, so numbers must be converted with str() first. f-strings let you place a variable directly inside curly brackets without converting it. f-strings are usually recommended because they need less code and are easier to read.

8. Practical: In IDLE, create a new file called sdd2_greeting.py. Write a program that asks the user for their name and age (converted to an integer), then prints a greeting using an f-string that includes both. Test it with your own name and age. Record your output below. TYPE 3

name = input("What is your name? ")
age = int(input("How old are you? "))
print(f"Hello {name}, you are {age} years old.")


Expected output (example): Hello Sam, you are 14 years old.

9. Practical: In sdd2_greeting.py, extend your program to also print how many characters are in the user's name, using len(). Test it and record your output. TYPE 3

print(f"Your name has {len(name)} characters.")

Expected output for "Sam": Your name has 3 characters.
Teacher notes — Shift+T to hide

Suggested timing: 55 minutes. Warm up 5 min; notes 15 min; examples 10 min; now you try 5 min; task set 20 min.

Key misconception to address: pupils assume input() "knows" what type they meant to type — reinforce that Python only sees characters, never intent.

Live demo suggestion: run age = input("Age: ") then print(age + 1) live and let pupils read the TypeError before fixing it together.

Extension idea (S5 / N5-bridging, non-assessed): briefly show string indexing/slicing (name[0]) as a "look ahead" — keep clearly separate from the assessed task set, as it is beyond N3/N4 scope.

Assessment standards covered: N3 O2.1; N4 O1.2, O1.3, O2.2.