String Operations (SQA National 5 Computing Science): Revision Note

Exam code: X816 75

Robert Hampton

Written by: Robert Hampton

Reviewed by: James Woodhouse

Updated on

String operations

What are string operations?

  • String operations are programming techniques used to modify, analyse, or extract information from a string

  • Examples of string operations include:

    • Case conversion (modify)

    • Length (analyse)

    • Substring (extract)

    • Concatenation (modify)

Case conversion

  • The ability to change a string from one case to another, for example, lower case to upper case

Operation

SQA pseudocode

Python equivalent

Output

Uppercase

DECLARE name INITIALLY "Sarah"
SEND UCASE(name) TO DISPLAY

name = "Sarah"
print(name.upper())

SARAH

Lowercase

DECLARE name INITIALLY "SARAH"
SEND LCASE(name) TO DISPLAY

name = "SARAH"
print(name.lower())

sarah

Length

  • The ability to count the number of characters in a string, for example, checking whether a password meets the minimum requirement of 8 characters

SQA pseudocode

Python equivalent

Output

DECLARE password INITIALLY "letmein"
SEND LENGTH(password) TO DISPLAY

password = "letmein"
print(len(password))

7

DECLARE password INITIALLY "letmein"
IF LENGTH(password) >= 8 THEN
SEND "Password accepted" TO DISPLAY
ELSE
SEND "Password too short" TO DISPLAY
END IF

password = "letmein"
if len(password) >= 8:
print("Password accepted")
else:
print("Password too short")

Password too short

Substring

  • The ability to extract a sequence of characters from a larger string, for example, for validation or combining with other strings

  • In SQA pseudocode, a substring starts at position 1 (not 0 as in Python)

SQA pseudocode

Python equivalent

Output

DECLARE word INITIALLY "Revision"
SEND SUBSTRING(word, 1, 3) TO DISPLAY

word = "Revision"
print(word[0:3])

Rev

DECLARE word INITIALLY "Revision"
SEND SUBSTRING(word, 3, 6) TO DISPLAY

word = "Revision"
print(word[2:6])

visi

Concatenation

  • The ability to join two or more strings together to form a single string

  • In SQA pseudocode, concatenation uses the ampersand (&) operator to join strings together

  • In Python, the plus (+) operator is used

Operation

SQA pseudocode

Python equivalent

Output

Concatenating two strings

DECLARE FName INITIALLY "Sarah"
DECLARE SName INITIALLY "Jones"
SET FullName TO FName & SName
SEND FullName TO DISPLAY

FName = "Sarah"
SName = "Jones"
FullName = FName + SName
print(FullName)

SarahJones

Concatenating two strings with a space

DECLARE FName INITIALLY "Sarah"
DECLARE SName INITIALLY "Jones"
SET FullName TO FName & " " & SName
SEND FullName TO DISPLAY

FName = "Sarah"
SName = "Jones"
FullName = FName + " " + SName
print(FullName)

Sarah Jones

Concatenating text and variables

DECLARE name INITIALLY "Sarah"
SEND "Hello, " & name TO DISPLAY

name = "Sarah"
print("Hello, " + name)

Hello, Sarah

Worked Example

A program is designed to output a welcome message using the user’s name.

Complete the missing line of code so that the output will be Welcome, Jordan

DECLARE name INITIALLY "Jordan"

SEND __________________________ TO DISPLAY

[1]

Answer

  • Correct use of concatenation to join text and variable [1 mark]

SEND "Welcome, " & name TO DISPLAY

Unlock more, it's free!

Join the 100,000+ Students that ❤️ Save My Exams

the (exam) results speak for themselves:

Robert Hampton

Author: Robert Hampton

Expertise: Computer Science Content Creator

Rob has over 16 years' experience teaching Computer Science and ICT at KS3 & GCSE levels. Rob has demonstrated strong leadership as Head of Department since 2012 and previously supported teacher development as a Specialist Leader of Education, empowering departments to excel in Computer Science. Beyond his tech expertise, Robert embraces the virtual world as an avid gamer, conquering digital battlefields when he's not coding.

James Woodhouse

Reviewer: James Woodhouse

Expertise: Computer Science & English Subject Lead

James graduated from the University of Sunderland with a degree in ICT and Computing education. He has over 14 years of experience both teaching and leading in Computer Science, specialising in teaching GCSE and A-level. James has held various leadership roles, including Head of Computer Science and coordinator positions for Key Stage 3 and Key Stage 4. James has a keen interest in networking security and technologies aimed at preventing security breaches.