0% found this document useful (0 votes)
8 views21 pages

Programming String Manipulation

The document provides an overview of key programming concepts related to data types, string manipulation, file handling, and validation, essential for Edexcel GCSE Computer Science. It covers primitive data types such as integers, reals, booleans, and characters, along with string manipulation techniques like length checking, position finding, and concatenation. Additionally, it explains file handling operations and various validation techniques to ensure user input meets specified criteria.

Uploaded by

br4ala
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views21 pages

Programming String Manipulation

The document provides an overview of key programming concepts related to data types, string manipulation, file handling, and validation, essential for Edexcel GCSE Computer Science. It covers primitive data types such as integers, reals, booleans, and characters, along with string manipulation techniques like length checking, position finding, and concatenation. Additionally, it explains file handling operations and various validation techniques to ensure user input meets specified criteria.

Uploaded by

br4ala
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Edexcel GCSE Computer Your notes

Science
Data Types & Data Structures
Contents
Programming Primitive Data Types
Programming String Manipulation
Programming File Handling
Programming Validation
Programming Authentication

© 2026 Save My Exams, Ltd. Get more and ace your exams at [Link] 1
Programming Primitive Data Types
Your notes

Programming Primitive Data Types


Section 1 covers the basics of different data types
The exam requires students to be able to write programs that make use of all primitive
data types
To recap, the main data types are:
Integer
Real
Boolean
Character

Programming integers
An integer is a whole number (negative or positive)
An example of an integer is 5
number = 5

numberOne = int(input("Enter a number"))

Programming reals
A real (also known as a float) is a decimal number
An example of a real is 3.4
realNumber = 3.4

price = float(input("Enter a number"))

Programming Boolean
A Boolean (also known as a bool) can be either True or False
An example of a bool is True
lightSensor = bool()

lightSensor = True

Programming characters
A character (also known as a char) is a single letter, number or symbol
An example of an character is "a"
firstNameInitial = str()

© 2026 Save My Exams, Ltd. Get more and ace your exams at [Link] 2
firstNameInitial = "a"

Your notes

© 2026 Save My Exams, Ltd. Get more and ace your exams at [Link] 3
Programming String Manipulation
Your notes

Programming String Manipulation


What is string manipulation?
String manipulation is the use of programming techniques to modify, analyse or extract
information from a string
Examples of string manipulation include:
Length (analyse)
Position (analyse)
Substrings (extract)
Case conversion (modify)
Concatenation (modify)

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 Python Output

Length Password = "letmein" 7


print(len(Password))

Password = "letmein" "Password too short"


if len(Password) >= 8:

print("Password accepted")

else:

print("Password too short")

Position
Position refers to the index or location of a character in a string
positions in strings, just like arrays, are 0 indexed (the first value is 0, not 1)
A position of a certain word of character from a string can be found using the following:
words = "Hello, World!"
Find the position of the letter e
print([Link]('e'))

© 2026 Save My Exams, Ltd. Get more and ace your exams at [Link] 4
The output would be 1
Substring Your notes
Substring and slicing perform similar tasks in programming
Substring is the ability to extract a sequence of characters from a larger string 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 a specific start and end to slice
out the desired characters
Substring is 0 indexed

Function Python Output

Substring string[start character : end character]

Word = "Revision" "vis"


print(Word[2:5])

left(number of characters)

Word = "Revision" "Revi"


print(Word[:4])

right(number of characters)

Word = "Revision" "sion"


print(Word[4:])

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

Function Python Output

Uppercase Name = "Sarah" "SARAH"


print([Link]())

Lowercase Name = "SARAH" "sarah"


print([Link]())

© 2026 Save My Exams, Ltd. Get more and ace your exams at [Link] 5
Title case Book = "inspector calls" "Inspector Calls"
print([Link]()) Your notes

Concatenation
The ability to join two or more strings together to form a single string
Concatenation uses the '+' operator to join strings together

Function Python Output

Concatenation FName = "Sarah" "SarahJones"


SName = "Jones"

FullName = FName + SName

print(FullName)

FName = "Sarah" "Sarah Jones"


SName = "Jones"

FullName = FName + " " + SName

print(FullName)

Name = "Sarah" "Hello, Sarah"


print("Hello, " + Name)

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:

© 2026 Save My Exams, Ltd. Get more and ace your exams at [Link] 6
Initial + First 3 letters of last name + year
For example, a student named David Hamilton who started in 2024 would have the
username: Your notes
DHam2024
The algorithm has been started below:

Line Algorithm

01 FName = input("Enter first name")

02 LName = input("Enter last name")

03 year = input("Enter year")

04 username =

05 print(username)

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

Line Algorithm

01 FName = input("Enter first name")

02 LName = input("Enter last name")

03 year = input("Enter year")

04 username = [Link](0,1) + [Link](0,3) + year

05 print(username)

[Link](0,1) 1 mark
[Link](0,3) 1 mark
username = [Link](0,1) + [Link](0,3) + year 1 mark

© 2026 Save My Exams, Ltd. Get more and ace your exams at [Link] 7
Programming File Handling
Your notes

Programming File Handling


What is file handling?
File handling is the use of programming techniques to work with information stored in
text files
Examples of file handing techniques are:
opening text files
reading text files
writing text files
closing text files

Concept Python

Open file = open("[Link]","r")

Close [Link]()

Read line [Link]()

Write line [Link]("Oranges")

End of file endOfFile = False

Create a new file file = open("[Link]","w")

Append a file file = open("[Link]","a")

The same approach for opening and writing to comma separated value files (CSV) can
be used
When opening a CSV file, the file extension would change to ".csv"
file = open("[Link]","r")

What are the differences when file handling?


Concept What it means

Open Opens the file in memory to be used in the program

© 2026 Save My Exams, Ltd. Get more and ace your exams at [Link] 8
Close Closes the file

This must be done to store any content that has been written or appended to it Your notes

Read Opens the file in read-only mode

The contents can be read but not changed

Write Opens the file in write mode

The contents of the file will be over-written

No prior content in the file will be saved

Append a file Opens the file in append mode

New data will be added to the end of the file

Previous data in the file will remain

Python example (reading data from a text file)


Employees Text file

file = open("[Link]", "r") # open file in read mode Greg


endOfFile = False # set end of file to false Sales
while not endOfFile: # while not end of file 39000
name = [Link]() # read line 1 43
department = [Link]() # read line 2 Lucy
salary = [Link]() # read line 3 Human resources
age = [Link]() # read line 4 26750
28
print("Name: ", name) # print name
Jordan
print("Department: ", department) # print department
Payroll
print("Salaray: ", salary) # print salary
45000
print("age: ", age) # print age
31
if name == "": # if name is empty
endOfFile = True # set end of file to true

[Link]() # close file

Python example (reading data from a csv file)


Employees CSV file

import csv Greg, Sales, 39000, 43


Lucy, Human resources, 26750, 28
# Open the CSV file Jordan, Payroll, 45000, 31

© 2026 Save My Exams, Ltd. Get more and ace your exams at [Link] 9
with open('[Link]', 'r') as csv_file:

csv_reader = [Link](csv_file) Your notes


# Iterate over each row in the CSV file
for row in csv_reader:

print(row)

Python example (writing new data to a text file)


Employees Text file

file = open("[Link]", "a") # open file in append mode Greg


[Link]("Polly\n") # write line (\n for new line) Sales
[Link]("Sales\n") 39000
[Link]("26000\n") 43
[Link]("32\n") Lucy
Human resources
[Link]() # close file
26750
28
Jordan
Payroll
45000
31
Polly
Sales
26000
32

Python example (writing new data to a csv file)


Employees CSV file

import csv John, Sales, 50000, 30,


# Sample data to write to CSV Jane, Marketing,
60000, 35
employees_data = [
Alice, Engineering,
['John', 'Sales', 50000, 30], ['Jane', 'Marketing', 60000, 35], ['Alice',
70000, 40
'Engineering', 70000, 40]

# Write data to CSV file


with open('[Link]', 'w', newline='') as csv_file:

csv_writer = [Link](csv_file)

© 2026 Save My Exams, Ltd. Get more and ace your exams at [Link] 10
csv_writer.writerows(employees_data)

print("Data has been written to [Link]") Your notes

Examiner Tips and Tricks


When opening files it is really important to make sure you use the correct letter in the
open command
"r" is for reading from a file only
"w" is for writing to a new file, if the file does not exist it will be created. If a file with
the same name exists the contents will be overwritten
"a" is for writing to the end of an existing file only
Always make a backup of text files you are working with, one mistake and you can lose
the contents!

Worked Example
Use pseudocode to write an algorithm that does the following :
Inputs the title and year of a book from the user.
Permanently stores the book title and year to the existing text file [Link] [4]
How to answer this question
Write two input statements (title and year of book)
Open the file
Write inputs to file
Close the file
Example answer
title = input("Enter title")

year = input("Enter year")

file = open("[Link]")

[Link](title)

[Link](year)

[Link]()

Marks
title = input("Enter title") 1 mark for both

year = input("Enter year")


file = open("[Link]") 1 mark
[Link](title) 1 mark for both

© 2026 Save My Exams, Ltd. Get more and ace your exams at [Link] 11
[Link](year)
[Link]() 1 mark
Your notes

© 2026 Save My Exams, Ltd. Get more and ace your exams at [Link] 12
Programming Validation
Your notes

Programming Validation
What is validation?
Validation is code which is used to check that an input from a user is acceptable and
that it matches the requirements of the program
There are 5 main categories of validation which can be carried out on fields and data
types, these are:
Length check
Range check
Presence check
Pattern check
There can be occasions where more than one type of validation will be used on a field
An example of this could be a password field which could have a length, presence and
type check on it
An effective way of programming validation can be using while loops that terminate
once the user has met the criteria

© 2026 Save My Exams, Ltd. Get more and ace your exams at [Link] 13
Your notes

Length check
Checks the length of a string
An example is ensuring that a password is 8 or more characters in length
Code example

password_length = len(password)

while password_length < 8:


password = input("Enter a password which is 8 or more characters")

Range check
Ensures the data entered as a number falls within a particular range
An example is checking a user's age has been entered and falls between the digits of 0-
100
Code example

age = int(input("Enter your age"))

© 2026 Save My Exams, Ltd. Get more and ace your exams at [Link] 14
while age < 0 or age > 100:
age = int(input("Enter your age, ensure it is between 0-100"))
Your notes

Presence check
Looks to see if any data has been entered in a field
An example is checking that a user has entered a name when registering for a website
Code example

name = input("Enter your name")

while name == "":


name = input("You must enter your name here")

Pattern check
Ensures that the data has been entered in the correct pattern / format
An example would be ensuring that an email includes the @ symbol and a full stop (.)
Code example

email = input("Enter your email address")

while "@" not in email or "." not in email:


email = input("Please enter a valid email address")

This could be extended further and made a little more complex by ensuring that the data
entered into a program follows the set format
In the example below, the user must enter a postcode in the format of LL00 0LL where L
is a letter and 0 is a number
This example uses a length check and string slicing covered in the previous revision note
Code example

valid_postcode = False

while not valid_postcode:

postcode = input("Enter a postcode (in the format LL00 0LL): ")

# Check if the length is correct


if len(postcode) != 8:

print("Invalid postcode length. Please enter again.")

continue

© 2026 Save My Exams, Ltd. Get more and ace your exams at [Link] 15
# Check if the format is correct
if not (postcode[:2].isalpha() and Your notes
postcode[2:4].isdigit() and

postcode[4] == ' ' and

postcode[5].isdigit() and

postcode[6:].isalpha()):

print("Invalid postcode format. Please enter again.")

continue

print("Valid postcode entered:", postcode)

valid_postcode = True

Worked Example
A car dealership uses a computer system to record details of the cars that it has for
sale. Each car has a make, model, age and number of miles driven.
The car dealership only sells cars that have fewer than 15,000 miles and are 10 years
old or less.
Write an algorithm that will:
Ask the user to enter the number of miles and the age of a car
Validate the input to check that only sensible values that are in the given range are
entered
Output True if valid data has been entered or False if invalid data has been
entered [4]
How to answer this question
When answering any algorithm question, ask yourself:
What inputs and outputs do I need?
Do I need to do any calculations or comparisons?
Do I need to use selection or iteration?
Do I need to use a function or procedure?
Re-read the algorithm question working through the criteria given
Programming Skill Algorithm

Inputs Miles
Age

Outputs True or False

© 2026 Save My Exams, Ltd. Get more and ace your exams at [Link] 16
Calculations / Check for valid mileage
Comparisons Check for valid age
Your notes
Selection or Iteration Selection is needed (If age <10 and miles <
15000)
Iteration is not needed

Function or Procedure Not needed

Answer: i)1 mark per bullet, max 4


Miles and age input separately
Checks for valid mileage
Checks for valid age
Checks both are greater than / greater than equal to zero
…correctly outputs both True and False
Example Answer:
miles = int(input("enter miles driven"))
age = int(input("enter age of car"))
valid = True
if miles > 15000 or miles < 0 :
valid = False
elif age > 10 or age < 0 :
valid = False
print(valid)

© 2026 Save My Exams, Ltd. Get more and ace your exams at [Link] 17
Programming Authentication
Your notes

Programming Authentication
What is Authentication?
Authentication is the process of ensuring that a system is secure by asking the user to
complete tasks to prove they are an authorised user of the system
Authentication is done because bots can submit data in online forms
Authentication can be done in several ways, these include
Usernames and Passwords
CAPTCHA
Other methods that programmers can do to authenticate the user include
Allowing users to recover passwords via email links and SMS codes
Encrypting data

© 2026 Save My Exams, Ltd. Get more and ace your exams at [Link] 18
Your notes

How can authentication be programmed?

© 2026 Save My Exams, Ltd. Get more and ace your exams at [Link] 19
Authentication of usernames and passwords can be programmed in several ways,
including using lists, databases and encryption
Your notes
Students are required to be able to program looking up usernames and passwords using
a list/array
To do this successfully, a 2-dimensional list/array will be used

Python example

# Set a Boolean flag to track if the username is found


found = False

# List of usernames and corresponding passwords


usernames = [

["Dave", "1"],

["Steve", "2"],

["James", "3"],

["Alice", "4"],

["Stephanie", "5"]

# Ask the user to input their username


user = input("Enter your username: ")

# Loop through each username in the list


for i in range(len(usernames)):

# Check if the entered username matches any in the list


if user == usernames[i][0]:

found = True # Set the flag to indicate the username is found

password = input("Enter your password: ") # Ask for the password

# Check if the entered password matches the password associated with the username
if password == usernames[i][1]:

print("Welcome")

else:

print("Wrong password")

# Check if the username was not found


if not found:

© 2026 Save My Exams, Ltd. Get more and ace your exams at [Link] 20
print("User not found")

Your notes

© 2026 Save My Exams, Ltd. Get more and ace your exams at [Link] 21

You might also like