String Handling (Cambridge (CIE) O Level Computer Science)

Revision Note

Robert Hampton

Written by: Robert Hampton

Reviewed by: James Woodhouse

String Handling

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:

    • Case conversion (modify)

    • Length (analyse)

    • Substrings (extract)

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 UCASE(Name)

Name = "Sarah"

print(Name.upper())

"SARAH"

Lowercase

Name ← "SARAH"

OUTPUT LCASE(Name)

Name = "SARAH"

print(Name.lower())

"sarah"

Length

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

Pseudocode

Python

Output

Password ← "letmein"

OUTPUT LENGTH(Password)

Password = "letmein"

print(len(Password))

7

Password ← "letmein"

IF LENGTH(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 has a start position of 1 in pseudocode and 0 in Python

Pseudocode

Python

Output

Word ← "Revision"

OUTPUT SUBSTRING(Word, 1, 3)

Word = "Revision"

print(Word[0:2])

"Rev"

Word ← "Revision"

OUTPUT SUBSTRING(Word, 3, 6)

Word = "Revision"

print(Word[2:5])

"visi"

Worked Example

The function Length(x) finds the length of a string x. The function substring(x,y,z) finds a substring of x starting at position y and z characters long. The first character in x is in position 1.

Write pseudocode statements to:

  • Store the string “Save my exams” in x

  • Find the length of the string and output it

  • Extract the word exams from the string and output it

[6]

Answer

X ← "Save my exams"

[1] for storing string in X

OUTPUT LENGTH(X)

[1] for calling the function length. [1] for using the correct parameter X

Y ← 9

Z ← 5

OUTPUT SUBSTRING(X,Y,Z)

[1] for using the substring function.

[1] for correct parameters

[1] for outputting length and substring return values

Last updated:

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

Sign up now. It’s free!

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

the (exam) results speak for themselves:

Did this page help you?

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

Author: James Woodhouse

Expertise: Computer Science

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.