String Manipulation (AQA GCSE Computer Science): Revision Note

Exam code: 8525

James Woodhouse

Written by: James Woodhouse

Reviewed by: Lucy Kirkham

Updated on

String Manipulation

What is string manipulation?

  • String manipulation or string handling is the use of programming techniques to modify, analyse or extract information from a string

  • Examples of string manipulation include:

    • Case conversion (modify)

    • Length (analyse)

    • Substrings (extract)

    • Concatenation (modify)

    • ASCII conversion (analyse)

Case conversion

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

Function

Pseudocode

Python

Output

Uppercase

Name ← "Sarah"

OUTPUT TOUPPER(Name)

Name = "Sarah"

print(Name.upper())

"SARAH"

Lowercase

Name ← "SARAH"

OUTPUT TOLOWER(Name)

Name = "SARAH"

print(Name.lower())

"sarah"

Title case

Book ← "inspector calls"

OUTPUT TOTITLE(Book)

Book = "inspector calls"

print(Book.title())

"Inspector Calls"

Length

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

Function

Pseudocode

Python

Output

Length

Password ← "letmein"

OUTPUT LEN(Password)

Password = "letmein"

print(len(Password))

7

Password ← "letmein"

IF LEN(Password) >= 8 then

OUTPUT "Password accepted"

ELSE

OUTPUT "Password too short"

ENDIF

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 in order to be used by another function in the program, for example, data validation or combining it with other strings

  • Extracting substrings is performed using 'slicing', using specific start and end to slice out the desired characters

  • Substring is 0 indexed (first value is 0 not 1)

Function

Pseudocode

Python

Output

 Substring


substring(starting character, number of characters)

string[start character : end character]

 

Word ← "Revision"

OUTPUT SUBSTRING(Word,2,3)

Word = "Revision"

print(Word[2:5])

"vis"

left(number of characters)

 

 

Word ← "Revision"

OUTPUT LEFT(Word,4)

Word = "Revision"

print(Word[:4])

"Revi"

right(number of characters)

 

 

Word ← "Revision"

OUTPUT RIGHT(Word,4)

Word = "Revision"

print(Word[4:])

"sion"

Concatenation

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

  • Concatenation uses the '+' operator to join strings together

Function

Pseudocode

Python

Output

Concatenation

FName ← "Sarah"

SName ← "Jones"

FullName ← FName + SName

OUTPUT(FullName)

FName = "Sarah"

SName = "Jones"

FullName = FName + SName

print(FullName)

"SarahJones"

FName ← "Sarah"

SName ←"Jones"

FullName ← FName + " " + SName

OUTPUT(FullName)

FName = "Sarah"

SName = "Jones"

FullName = FName + " " + SName

print(FullName)

"Sarah Jones"

Name ← "Sarah"

OUTPUT("Hello, " + Name)

Name = "Sarah"

print("Hello, " + Name)

"Hello, Sarah"

ASCII conversion

  • The ability to return an ASCII character from a numerical value and vice versa

Function

Pseudocode

Python

Output

ASCII conversion

CHAR_TO_CODE(A)

print(ord("A"))

"65"

CODE_TO_CHAR(97)

print(chr(97))

"a"

What is casting?

  • Casting is when you convert one data type to another data type

Example

  • The following Python program is used to capture a users age to determine if they are old enough to vote

Line

Python code

01

age = input("Enter age")

02

if age >= 18:

03

print("Old enough to vote")

04

else:

05

print("Too young to vote")

  • In this example, on line 01, no specific data type is requested

  • By default the data type is stored as 'string'

  • On line 02, a run-time error would occur because age is stored as a string and is being compared to an integer value in the selection statement

  • Casting the age from a string to an integer would solve the error

Line

Python code

01

age = input("Enter age")

02

if int(age) >= 18:

03

print("Old enough to vote")

04

else:

05

print("Too young to vote")

  • In the corrected code, casting is highlighted in green

String Conversion Operations

Conversion

Example

Output

From String to Integer

STRING_TO_INT ("10")

10

From Integer to String

INT_TO_STRING (5)

"5"

From String to Real

STRING_TO_REAL ("16.5)

16.5

From Real to String

REAL_TO_STRING (5.4)

"5.4"

Examiner Tips and Tricks

Remember that the '+' operator is used for concatenation of strings BUT is also the mathematical operator for addition

It is important to remember, that the same operator symbol performs different roles on different types of data (integer/string)

Worked Example

A school wants to use a program to take a students first name, last name and year of entry as inputs and use them to create a username

They want the username to follow the rule:

  • Initial + First 3 letters of last name + year

For example, a student named David Hamilton who started in 2024 would have the username:

  • DHam2024

The algorithm has been started below:

Table showing an algorithm with five lines. Lines 1-3 take user inputs for first name, last name, and year respectively; Lines 4-5 await username creation and display.

Use string manipulation to complete line 04 to create the username [3]

How to answer this question

  • What techniques do we need to use to create the username? substring to extract the parts of the first and last name

  • Concatenation to join them together

Answer

Table listing an algorithm in five lines to create a username by combining parts of first and last names with a given year and printing the result.

Guidance

  • FName.substring(0,1) 1 mark

  • LName.substring(0,3) 1 mark

  • username = FName.substring(0,1) + LName.substring(0,3) + year 1 mark

You've read 0 of your 5 free revision notes this week

Unlock more, it's free!

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

the (exam) results speak for themselves:

Did this page help you?

James Woodhouse

Author: 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.

Lucy Kirkham

Reviewer: Lucy Kirkham

Expertise: Head of STEM

Lucy has been a passionate Maths teacher for over 12 years, teaching maths across the UK and abroad helping to engage, interest and develop confidence in the subject at all levels.Working as a Head of Department and then Director of Maths, Lucy has advised schools and academy trusts in both Scotland and the East Midlands, where her role was to support and coach teachers to improve Maths teaching for all.