Exam code:1CP2
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 |
|
7 |
|
|
“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 eprint(words.index('e')) -
The output would be 1
-
Substring
-
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] |
|
|
|
“vis” |
|
|
left(number of characters) |
|
|
|
|
“Revi” |
|
|
right(number of characters) |
|
|
|
|
“sion” |
Case conversion
-
The ability to change a string from one case to another, for example, lower case to upper case
|
Function |
Python |
Output |
|---|---|---|
|
Uppercase |
|
“SARAH” |
|
Lowercase |
|
“sarah” |
|
Title case |
|
“Inspector Calls” |
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 |
|
“SarahJones” |
|
|
“Sarah Jones” |
|
|
|
“Hello, Sarah” |
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:
|
Line |
Algorithm |
|
01 |
|
|
02 |
|
|
03 |
|
|
04 |
|
|
05 |
|
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 |
|
|
02 |
|
|
03 |
|
|
04 |
|
|
05 |
|
-
FName.substring(0,1)1 mark -
LName.substring(0,3)1 mark -
username = FName.substring(0,1) + LName.substring(0,3) + year1 mark
Responses