Study Guide

String Handling

How a program works with text: finding the length of a string, picking out part of it with SUBSTRING, changing case with UCASE and LCASE, and using a loop to count vowels, reverse a word or build initials, with the difference between counting from 1 and from 0.

Lesson 5.8 Algorithms and Programming

For: Cambridge IGCSE Computer Science learners, or Key Stage 3 learners who have met loops and selection (about ages 13 to 14)

Stage: Think

A username from a name

A fictional school gives every pupil a computer username. The rule is: the first letter of the first name, followed by the first three letters of the surname, all in lower case.

Think it through

What is the username for a pupil called Amira Khan? Then say what a program would have to be able to do with the text “Amira” and “Khan” to follow the rule.

Show a model answer

The username is akha: “a” from Amira, then “kha” from Khan, in lower case. To follow the rule a program has to pick out letters from a given place in a piece of text (the first letter, the first three letters), join the pieces together and turn capital letters into lower case.

Stage: Discover

Where is each character?

Text in a program is a string: a row of characters, such as the letters of a word. Every character has a place in the row.

Think it through

In the word SUNSET, which place is the letter N in, counting from the left? How many characters does the whole word have?

Show what happens

N is the third character, and the word has 6 characters.

The number of characters in a string is its string length. Cambridge pseudocode finds it with LENGTH. The place of a character is its position, and the first character is at position 1. A substring is a part of a string, taken out with SUBSTRING(string, start, length): start is the position of the first character wanted, and length is how many characters to take.

Word ← "Computing"
OUTPUT LENGTH(Word)
OUTPUT SUBSTRING(Word, 1, 3)

This outputs 9 (the word has nine characters) and then Com (three characters, starting at position 1). A string can include spaces and punctuation, and each counts as a character.

The third number in SUBSTRING is a length, not the position where the substring ends. SUBSTRING(Word, 4, 3) takes three characters starting at position 4, which are positions 4, 5 and 6. It does not mean “from position 4 to position 3”.

Working out string expressions, with Word ← “Computing”
ExpressionResult
LENGTH(Word)
SUBSTRING(Word, 1, 3)Com
SUBSTRING(Word, 4, 3)
SUBSTRING(Word, 7, 3)
SUBSTRING(Word, 9, 1)

    Show the completed table
    ExpressionResult
    LENGTH(Word)9
    SUBSTRING(Word, 1, 3)Com
    SUBSTRING(Word, 4, 3)put
    SUBSTRING(Word, 7, 3)ing
    SUBSTRING(Word, 9, 1)g

    The second row is given as an example. Each answer is a string, so its case matters when a program compares it.

    Check your understanding

    What does SUBSTRING(“Cinema”, 3, 2) return?
    Show the answer

    Answer: C. ne

    Start at the position given, then take that many characters.

    Stage: Explain

    Capital letters count

    A fictional quiz asks “Do you want to play again?” and moves on if the player types yes.

    Think it through

    One player types yes. Another types YES. A third types Yes. A program that tests Reply = “yes” accepts only the first. Why do you think a computer treats these as different, and how could the program accept all three?

    Show a model answer

    A computer stores a capital Y and a small y as different characters, so “YES” and “yes” are not the same string, even though a person reads them as the same word. The program can change the reply to one case before it compares, so that all three become the same string.

    UCASE returns a string with every letter in upper case, and LCASE returns it in lower case. They do not change the variable by themselves; they return a new string, which you store with an assignment.

    INPUT Reply
    Reply ← LCASE(Reply)
    IF Reply = "yes"
      THEN
        OUTPUT "Starting again"
      ELSE
        OUTPUT "Goodbye"
    ENDIF

    UCASE("Happy") gives HAPPY, and LCASE("Happy") gives happy. The lines Reply ← LCASE(Reply) and the test after it make YES, Yes and yes all give “Starting again”.

    1. Take the first letter of the first name. SUBSTRING(First, 1, 1): start at position 1 and take one character, which is A.
    2. Take the first three letters of the surname. SUBSTRING(Surname, 1, 3): start at position 1 and take three characters, which are Kha.
    3. Join them. The & operator joins two strings end to end: A & Kha is AKha.
    4. Change the case last. LCASE of AKha is akha, the username. Doing it once at the end is simpler than changing each piece.
    INPUT First
    INPUT Surname
    Start ← SUBSTRING(First, 1, 1)
    Middle ← SUBSTRING(Surname, 1, 3)
    Both ← Start & Middle
    Username ← LCASE(Both)
    OUTPUT Username

    Check your understanding

    Why does the quiz change Reply to lower case before the IF test?
    Show the answer

    Answer: B. Because YES, Yes and yes are different strings, so one test would match only one.

    Change both sides of a comparison to the same case, so that capitals do not decide the answer.

    Stage: Explain

    Position 1, or position 0?

    Think it through

    In some countries the lift button for the ground floor is 0, and the next one up is 1. In others the ground floor is 1. If two people give directions (“second floor”), what could go wrong? What would help?

    Show a model answer

    They might mean different floors, because one counts the ground floor as the first and the other counts it as zero. It helps to agree where counting starts, or to give the name or number printed on the button.

    Programming languages face the same question for the characters of a string. Cambridge pseudocode counts from 1: the first character is at position 1, so SUBSTRING("Hello", 2, 1) is e. Many real languages, including Python, count from 0: the first character is at index 0, so the e of “Hello” is at index 1. The syllabus notes that, in a program, the first character can be position zero or one, so always check which one a language or an exam question uses.

    Do not confuse: Counting the characters of “Hello”

    First character HCambridge pseudocode
    Position 1. The e is at position 2.SUBSTRING(Word, 2, 1) gives e
    First character HPython
    Index 0. The e is at index 1.word[1] gives e

    The characters are the same; only the numbering differs. The length of the string, 5, is the same in both.

    Check your understanding

    A program is written in a language that counts the first character as position 0. In the word “Planet”, which position holds the letter n?
    Show the answer

    Answer: A. 3, because P is 0, l is 1, a is 2 and n is 3.

    The same character gets a different number in each system; the characters themselves do not move.

    Stage: Predict

    A loop that looks at every character

    To look at each character in turn, a loop counts positions from 1 to the length of the string, and takes out one character at a time with SUBSTRING(Word, Position, 1). A fictional spelling game wants to know how many vowels (a, e, i, o, u) are in a word.

    Vowels ← 0
    INPUT Word
    FOR Position ← 1 TO LENGTH(Word)
      Letter ← SUBSTRING(Word, Position, 1)
      Letter ← LCASE(Letter)
      IF Letter = "a" OR Letter = "e" OR Letter = "i" OR Letter = "o" OR Letter = "u"
        THEN
          Vowels ← Vowels + 1
      ENDIF
    NEXT Position
    OUTPUT Vowels

    Think it through

    Predict how many vowels the program counts for the word Cinema. Then say why the line Letter ← LCASE(Letter) is there.

    Show what happens

    Three: the i, the e and the a. The LCASE line makes a capital letter match the lower-case letters in the test, so that a word such as “Island” counts its capital I.

    1. Set the start. Vowels is 0 before the loop, as with any count.
    2. Work out the loop. Cinema has 6 characters, so Position goes 1, 2, 3, 4, 5, 6.
    3. Take one letter and tidy it. Position 1 gives C; LCASE makes it c. It is not a vowel, so Vowels stays 0.
    4. Test the vowels. Position 2 gives i, which is a vowel, so Vowels becomes 1. Positions 4 (e) and 6 (a) add the other two.
    5. Output. Vowels is 3.

    Now trace the word Island yourself. The Letter column shows the letter after LCASE.

    Following the vowel program for the word Island
    PositionLetter after LCASEIs it a vowel?Vowels
    Before the loop0
    1
    2
    3
    4
    5
    6

      Show the completed table
      PositionLetter after LCASEIs it a vowel?Vowels
      Before the loop0
      1iYes1
      2sNo1
      3lNo1
      4aYes2
      5nNo2
      6dNo2

      The program outputs 2.

      Check your understanding

      The program is run on the word Island with the line Letter ← LCASE(Letter) removed. What does it output?
      Show the answer

      Answer: D. 1, because the capital I no longer matches the lower-case i.

      Change the case before comparing, or the test has to list both cases.

      Stage: Explain

      Building a new string

      A program can build a new string by starting with an empty string (two quotation marks with nothing between them) and joining one character at a time on to it with &. To reverse a word, the loop must read the characters from the last to the first, which a FOR loop with STEP -1 does.

      1. Start empty. Reversed ← "", because nothing has been built yet.
      2. Count down. FOR Position ← LENGTH(Word) TO 1 STEP -1 visits positions 4, 3, 2, 1 for a four-letter word.
      3. Add each letter to the end. Reversed ← Reversed & Letter. Position 4 gives s, so Reversed is s; position 3 gives t, so Reversed is st; then sta; then stab.
      4. Output after the loop, when the new string is complete.
      Reversed ← ""
      INPUT Word
      FOR Position ← LENGTH(Word) TO 1 STEP -1
        Letter ← SUBSTRING(Word, Position, 1)
        Reversed ← Reversed & Letter
      NEXT Position
      OUTPUT Reversed

      Now trace the same program for the word stop.

      Following the reverse program for the word stop
      PositionLetterReversed
      Before the loop(empty)
      4
      3
      2
      1

        Show the completed table
        PositionLetterReversed
        Before the loop(empty)
        4pp
        3opo
        2tpot
        1spots

        The program outputs pots.

        Joining strings is also called concatenation. The syllabus’s pseudocode guide lists the string functions of this lesson but does not name a symbol for joining. This lesson uses &, the symbol in Cambridge’s A Level pseudocode guide; in an exam, follow whatever the question shows. Python joins with +. Writing several values after OUTPUT, separated by commas, only displays them one after another: it does not build a new string.

        Stage: Apply

        Spot the mistake

        Sam (fictional) wants to reverse a word but the program below outputs stop for the word stop.

        Reversed ← ""
        INPUT Word
        FOR Position ← 1 TO LENGTH(Word)
          Letter ← SUBSTRING(Word, Position, 1)
          Reversed ← Reversed & Letter
        NEXT Position
        OUTPUT Reversed

        Apply it

        What is the fault, and what is the fix?
        Show the answer

        Answer: C. The letters are joined in the order read, first to last; count down with STEP -1.

        Order matters: the order in which a loop reads the characters is the order in which they are joined.

        Priya (fictional) knows Python well. She writes a pseudocode loop for the vowel program as FOR Position ← 0 TO LENGTH(Word) - 1, with the same SUBSTRING(Word, Position, 1) line.

        Apply it

        What goes wrong when Priya’s loop is used in Cambridge pseudocode?
        Show the answer

        Answer: B. It asks for position 0, which does not exist, and never looks at the last character.

        Use the counting of the language you are writing in, for the loop and for the substring together.

        Stage: Apply

        Write one yourself

        A fictional school club needs initials for name badges. The program asks for a first name and a last name, typed in any case, and outputs the two initials in capitals.

        Think it through

        Write pseudocode that asks for a first name and a last name and outputs the initials in capitals. What should it output for maya and okafor?

        Show a model answer
        OUTPUT "First name?"
        INPUT First
        OUTPUT "Last name?"
        INPUT Last
        FirstInitial ← SUBSTRING(First, 1, 1)
        LastInitial ← SUBSTRING(Last, 1, 1)
        Initials ← FirstInitial & LastInitial
        Initials ← UCASE(Initials)
        OUTPUT Initials

        For maya and okafor, the program outputs MO. Changing the case once, after joining, copes with names typed in any case. The two prompts tell the user what to type.

        Go deeper The same ideas in Python
        word = "Computing"
        print(len(word))
        print(word[0:3])
        print(word[3:6])
        print(word[8])
        print(word.upper())
        
        first = input("First name? ")
        last = input("Last name? ")
        initials = first[0] + last[0]
        print(initials.upper())
        
        vowels = 0
        for letter in word.lower():
            if letter in "aeiou":
                vowels = vowels + 1
        print(vowels)
        • len(word) is LENGTH(Word), and it gives 9 for Computing.
        • Python counts from 0, so the first character is word[0] and the last is word[len(word) - 1] or, more simply, word[-1].
        • A slice such as word[3:6] takes the characters from index 3 up to, but not including, index 6, which gives put. To convert, SUBSTRING(Word, start, length) is word[start - 1 : start - 1 + length]: SUBSTRING(Word, 4, 3) becomes word[3:6].
        • word.upper() and word.lower() are UCASE and LCASE. Joining strings uses +.
        • Python can loop straight over the characters (for letter in word), so it does not always need a position. A string reversed with word[::-1] is a shortcut; the exam’s loop-and-join method is the one to practise.

        Stage: Consolidate

        Show what you know

        Think it through

        Explain to a friend how a program picks out part of a string, and what is different when you do it in Python. Use a word of your own as an example.

        Show a model answer

        A good answer says that SUBSTRING takes a start position and a length, with the first character at position 1 in Cambridge pseudocode, and gives an example, such as SUBSTRING("Planet", 2, 3) being lan. It says Python counts from 0 and uses a slice that stops before its end, so the same part is word[1:4]. It may add that LENGTH gives the number of characters and that LCASE and UCASE settle the case before a comparison.

        Apply it

        A program holds the string Word and needs its last character, whatever the length. Which expression gives it?
        Show the answer

        Answer: A. SUBSTRING(Word, LENGTH(Word), 1)

        In Cambridge pseudocode the last position equals the length, because counting starts at 1.

        • LENGTH(s) is the number of characters. SUBSTRING(s, start, length) takes length characters starting at position start; the first character is at position 1 in Cambridge pseudocode.
        • UCASE and LCASE return upper- and lower-case versions; use them to compare text without capitals getting in the way, or to tidy output.
        • A loop from 1 to LENGTH(Word) with SUBSTRING(Word, Position, 1) looks at each character in turn. Counting down with STEP -1 reads from the end, which is how to reverse.
        • Python counts from 0 and uses slices that stop before the end, so SUBSTRING(Word, 4, 3) is word[3:6].
        • Check the usual faults: a start position of 0, a length taken for an end position, a case difference in a comparison, and joining letters in the wrong order.

        Sources and scope

        • What this lesson is. The eighth lesson of the Algorithms and Programming module, written for this site. It covers point 8.1.4(e) of Cambridge IGCSE Computer Science 0478 (2026 to 2028, version 6), string handling with LENGTH, SUBSTRING, UCASE and LCASE, and the note that the first character can be position zero or one. Its curriculum links are provisional and imply no review or endorsement by any examination board.
        • Original material. The school usernames, the quiz, the spelling game, the club badges, Sam and Priya are fictional, written for this lesson. Every output was worked out by hand and checked with an equivalent Python program.
        • Simplified on purpose. Strings hold only ordinary characters. The joining operator & is not named in the 0478 pseudocode guide; it is taken from Cambridge’s A Level guide and explained as such. Searching within a string, comparing alphabetical order, and character codes are not taught here.
        • Left to later lessons. Procedures, functions and library routines, and arrays of strings.