SDD  ·  Software Design & Development

Input, Output & String Operations

Lesson SDD6 of 18 Approx 70 min PyCharm practical setup
Learning intentions
  • Describe the purpose of input and output in a program
  • Use SQA reference language for receiving input and sending output
  • Set up a simple Python project in PyCharm and run a program
  • Use Python input(), print(), casting, and + concatenation
  • Apply common string operations including length, concatenation, substring, and case conversion
Success criteria
  • I can write RECEIVE and SEND statements in SQA reference language
  • I can explain why keyboard input often needs converted before arithmetic
  • I can create, save, run, and fix a simple Python file in PyCharm
  • I can combine text and variables in output without causing a type error
  • I can trace and write code that uses common string operations
Warm up — what do you already know?

Answer before the lesson begins. These check prior knowledge from variables, data types and arithmetic.

1. Which data type is most appropriate for the value "Edinburgh"?

2. What is the result of "High" + "School" in SQA reference language?

3. Which expression correctly calculates the average of three variables?

Key vocabulary

input
Data entered into a program, often from the keyboard
output
Information sent from a program to the user, commonly shown on the display
prompt
A message that tells the user what to enter
casting
Converting data from one type to another, such as string to integer
concatenation
Joining strings together to make one longer string using the + operator at N5
str()
A Python function that converts a value to a string so it can be joined to text
length
The number of characters in a string, including spaces and symbols
substring
A smaller part of a string taken from a larger string
index
The position of a character in a string. Python starts counting at 0
method
A command attached to an object, such as name.upper()

Input, Output and String Operations

Programs need a conversation with the user

A program is useful when it can accept data, process it, and show a result. Input is data entering the program. At National 5 this usually means data typed using the keyboard. Output is information leaving the program. This is usually text displayed on the screen.

In SQA reference language, input and output have a very specific style. Output uses SEND ... TO DISPLAY. Input uses RECEIVE ... FROM (TYPE) KEYBOARD. The type in brackets matters because it tells the reader what kind of data is expected.

SEND "Enter your age: " TO DISPLAY
RECEIVE age FROM (INTEGER) KEYBOARD
SEND "You are " + age + " years old." TO DISPLAY

Python input and output

Python uses print() for output and input() for keyboard input. The message inside input() is called a prompt. It should be clear enough that the user knows exactly what to type.

name = input("Enter your name: ")
print("Hello " + name)

There is one crucial Python detail: input() always returns a string. Even if the user types 14, Python stores "14" unless you convert it. To do arithmetic, cast the input using int() for whole numbers or float() for decimal numbers.

age = int(input("Enter your age: "))
height = float(input("Enter your height in metres: "))

Using PyCharm for implementation

PyCharm is an Integrated Development Environment, or IDE. It gives you an editor, a run button, error highlighting, and a console in one place. For this course, use one project folder for SDD practice and create one Python file per lesson or task. A sensible file name for this lesson is sdd6_strings.py.

When you create a new PyCharm project, choose a plain Python project, keep the location somewhere you can find again, and make sure a Python interpreter is selected. Then create a new Python file, type your program, and run it using the green Run button or by right-clicking the file and choosing Run. The program output appears in the Run panel at the bottom.

Professional programmers run tiny tests often. Type a few lines, run them, check the output, then continue. This makes mistakes easier to find than writing a whole program before running it for the first time.

Combining text and variables

Output often needs to combine fixed text with variable values. At N5, use + for concatenation in both SQA reference language and Python. In Python, numbers must be converted with str() before they can be joined to text using +:

name = "Sam"
score = 12
print(name + " scored " + str(score) + " points")

For this course, use + when practising concatenation, and convert non-string values with str() first.

String operations

A string is a sequence of characters. Python can measure, transform, and slice strings. len(name) returns the length. name.upper() changes letters to uppercase. name.lower() changes letters to lowercase.

greeting = "hello"
print(greeting.upper())    # HELLO
print(len(greeting))       # 5

Python also lets you access characters by position. The first character is index 0, the second is index 1, and so on. A substring can be taken using slicing, for example code[0:3] gives characters from index 0 up to, but not including, index 3.

code = "N5CS2026"
print(code[0])     # N
print(code[0:4])   # N5CS
print(code[2:6])   # CS20
character index N 5 C S 2 0 2 6 0 1 2 3 4 5 6 7

Exam wording versus Python code

The exam may ask you to read or write SQA reference language, while implementation tasks ask you to write Python. Keep the two styles separate. RECEIVE age FROM (INTEGER) KEYBOARD is the exam design form. age = int(input("Enter age: ")) is the Python implementation. They represent the same idea, but they are not written the same way.

Worked examples

Example 1 — SQA input and output

A program asks for a first name and displays a greeting.

DECLARE firstName AS STRING INITIALLY ""
SEND "Enter your first name: " TO DISPLAY
RECEIVE firstName FROM (STRING) KEYBOARD
SEND "Hello " + firstName + "!" TO DISPLAY
1
The variable is declared as a string because a name is text.
2
The prompt tells the user what to enter before the program receives keyboard input.
3
The final SEND joins fixed text with the variable using +.
Example 2 — Python input with casting

A program asks for two marks and displays the total.

mark1 = int(input("Enter mark 1: "))
mark2 = int(input("Enter mark 2: "))
total = mark1 + mark2
print("Total mark: " + str(total))
1
input() returns a string, so each mark is converted using int().
2
The arithmetic line adds two integers. If the casts were missing, Python would join the text values instead.
3
str(total) converts the integer total to text so it can be joined to the message using +.
Example 3 — String operations

A program receives a username and creates a short code from the first three characters.

username = input("Enter username: ")
short_name = username[0:3].upper()
print("Your short code is " + short_name + str(len(username)))
1
input() receives the username as a string.
2
username[0:3] takes the first three characters. .upper() changes them to uppercase.
3
If the user enters morgan, the output is Your short code is MOR6.
Now you try

A Python program asks for a city name. It should display the name in uppercase, then display the number of characters in the name.

Answer the following:

  1. Write the Python line that receives city from the keyboard.
  2. Write the Python line that displays the city name in uppercase.
  3. Write the Python line that displays Length: followed by the number of characters.
  1. city = input("Enter city: ")
  2. print(city.upper())
  3. print("Length: " + str(len(city)))
Common mistakes
Forgetting that input() returns a string. age = input("Age: ") stores text. Use int() or float() before arithmetic.
Forgetting str() when joining text and numbers in Python. "Score: " + score causes a type error if score is an integer. Use "Score: " + str(score).
Leaving spaces out of concatenated messages. "Hello" + name produces HelloSam. Include a space inside the string: "Hello " + name.
Mixing exam reference language and Python. SEND and RECEIVE are for SQA reference language. print() and input() are for Python implementation.
Getting string indexes one place out. Python starts counting at 0, so the first character of "N5" is text[0], not text[1].
Exam tip

When an exam question asks you to write pseudocode/reference language, use the SQA wording: SEND, RECEIVE, and + for concatenation. When a practical task asks for Python, use print(), input(), casts, str(), and +. Marks are often lost because the answer is correct programming logic but written in the wrong language style for the question.

Task Set

Questions 1–5 are auto-checked. Questions 6–7 are self-marked. Questions 8–10 are practical PyCharm implementation tasks.

1. Which SQA reference language statement displays a message on the screen? TYPE 1

2. Which Python line correctly receives an integer age from the keyboard? TYPE 1

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

4. If word = "PYTHON", what is word[0:3]? TYPE 1

5. What is the output of print("Total: " + str(7 + 5))? TYPE 1

6. Write SQA reference language that asks the user to enter their town, receives it into a string variable called town, then displays You live in followed by the town. TYPE 2

DECLARE town AS STRING INITIALLY ""
SEND "Enter your town: " TO DISPLAY
RECEIVE town FROM (STRING) KEYBOARD
SEND "You live in " + town TO DISPLAY

7. Explain why this Python code causes a problem, then write a corrected version: score = input("Score: ") then total = score + 10. TYPE 2

The input is stored as a string, so Python cannot add the integer 10 to it. Corrected version:
score = int(input("Score: "))
total = score + 10

8. Practical: set up PyCharm for this lesson. Create or open an SDD practice project, create a new Python file called sdd6_strings.py, add the code below, run it, and record the output. TYPE 3

print("SDD6 ready")
print("Input, output and strings")
The Run panel should show:
SDD6 ready
Input, output and strings

9. Practical: in the same PyCharm file, write a program that asks for a first name and surname, then displays a username made from the first three letters of the first name and the first three letters of the surname, all lowercase. Test with Test User. TYPE 3

first_name = input("Enter first name: ").lower()
surname = input("Enter surname: ").lower()
username = first_name[0:3] + surname[0:3]
print("Username: " + username)
For Test User, the output is Username: tesuse.

10. Practical: write a Python program that asks for the price of an item and the quantity bought, then displays the total cost using + concatenation. Use float() for the price, int() for the quantity, and str() for the output. Test with price 2.50 and quantity 4. TYPE 3

price = float(input("Enter price: "))
quantity = int(input("Enter quantity: "))
total = price * quantity
print("Total cost: £" + str(total))
For price 2.50 and quantity 4, the output is Total cost: £10.0.
Teacher notes — Shift+T to hide

Suggested timing: 70 minutes. Warm up 5 min; notes and short live demos 20 min; PyCharm setup 15 min; worked examples 10 min; task set 20 min.

Key misconception to address: Python input() always returns a string. All pupils may think typing digits automatically creates an integer.

Live demo suggestion: Deliberately run score = input() followed by score + 10, read the type error, then fix it with int().

Extension question: Ask all pupils to improve the price task so it rejects negative quantities once selection has been taught.

SQA command words covered: identify, describe, explain, write, implement, test.