- 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()
- I can use
input()to store a user's response in a variable - I can convert user input to
intorfloatwhen needed - I can produce combined text and variable output with
print()
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
+ operator.int("5") converts the string "5" to the integer 5.print(), written as f"..." with variables inside { }.Input, Output and String Basics
Getting input from the user
input()User types textint() / float()Convert if maths is neededprint()Show the result clearlyPython treats keyboard input as text first. Convert it before adding, multiplying or comparing it as a number.
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
A program needs the user's name and age.
name = input("What is your name? ") — stored as a string, no conversion needed.age = int(input("How old are you? ")) — converted straight to an integer in one line.age can be used in a calculation, e.g. age + 1, without an error.Displaying a greeting using the name and age variables from Example 1.
print("Hello " + name + ", you are " + str(age) + ".")print("Hello", name, "you are", age)print(f"Hello {name}, you are {age}.") — usually the clearest to read and write.A program checks how long a password someone types is.
password = input("Enter a password: ")length = len(password) stores how many characters were typed.print(f"Your password is {length} characters long.")A quiz program contains this line: score = input("Enter your score out of 10: "), followed later by total = score + 5.
Answer the following:
- What data type is
scoreafter the first line runs? - What will happen when the second line,
total = score + 5, tries to run? - Rewrite the first line so the program works correctly.
scoreis a string, becauseinput()always returns a string.- Python will raise a
TypeError, because you cannot add a string and an integer together with+. score = int(input("Enter your score out of 10: "))— converting straight to an integer fixes the problem.
input(). Trying to use the result of input() directly in a calculation causes a TypeError, because it is a string.print("Score: " + score) fails if score is an integer — convert it with str(score) first, or use commas / an f-string instead.input(). Writing name = input() with nothing in the brackets leaves the user unsure what to type — always include a clear message.+ 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.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."
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
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
+ 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.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.