HelpContactFor teachers and institutes
>_StemPy Lab
Mock TestsClasses

>_StemPy Lab

Learn. Solve. Code the solutions. Test yourself.

Live examsFree mock exams, timed and rankedEveryone starts together, and your score and rank come the moment it ends.See the exams →

Learn

  • Physics
  • Chemistry
  • Mathematics
  • Biology
  • Python Programming
  • Statistics & Data Science
  • Computer Science
  • Classes
  • Groups

Practice

  • Problems
  • Quizzes
  • Mock Tests
  • Prepare for an Exam
  • Question Sets
  • Exams
  • Certificates
  • Python Labs
  • AI Practice Generator
  • Student-Created Tests

Community

  • Discussions
  • Resources
  • Notes
  • Glossary
  • Blog

Account

  • Log in
© 2026 senLearn. All rights reserved.HelpFor SchoolsAboutContactPrivacy PolicyTerms of UseRefund Policy
Procedures, Functions and Strings in Pseudocode
  1. 1Procedures, functions and parameters
  2. 2Library routines and string handling
  3. 3Maintainable programs and the long scenario question
  1. Computer Science
  2. /Procedures, Functions and Strings in Pseudocode
  3. /Library routines and string handling

Course 8.2·Lesson 2 of 3

Library routines and string handling

Every language comes with ready-made routines. This lesson uses the library routines of the syllabus, MOD, DIV, ROUND and RANDOM, and the four string routines LENGTH, SUBSTRING, UCASE and LCASE, in Cambridge pseudocode and in Python.

about 25 min1 simulation1 practice question
Lesson 2 of 3Show all lessonsHide
Procedures, Functions and Strings in Pseudocode
  1. 1Procedures, functions and parameters
  2. 2Library routines and string handling
  3. 3Maintainable programs and the long scenario question

Try the code and the simulations beside the lesson.

Routines you do not have to write

Nobody writes their own square-root routine. Languages come with a library: routines that are ready to call. The syllabus names the ones you must know. You met many of them in Python already: // and % in "Numbers and Operators", round() and random in the Python Essentials chapter "Modules: Toolboxes You Can Import".

MOD, DIV, ROUND and RANDOM

  • DIV(17, 5) returns 3, the whole part of the division.
  • MOD(17, 5) returns 2, the remainder.
  • ROUND(Value, Places) returns Value rounded to Places decimal places. Value is REAL and Places is an INTEGER.
  • RANDOM() returns a random number between 0 and 1, both included.
pseudocode
DECLARE Average : REAL
Average ← ROUND(17 / 3, 2)
OUTPUT Average

17÷3=5.666…17 \div 3 = 5.666\ldots17÷3=5.666…, so the output is 5.67. The syllabus gives this example of RANDOM: Value ← ROUND(RANDOM() * 6, 0) returns a whole number between 0 and 6. Multiplying by 6 stretches the range to 0 to 6, and rounding to 0 places makes it whole.

MOD has two common uses. MOD(Number, 2) = 0 is TRUE for an even number. MOD(Number, 10) is the last digit of a whole number.

String handling

A string is a row of characters. In Cambridge pseudocode the first character is usually at position 1. The syllabus allows position 0 or 1, so a question will say which it uses.

  • LENGTH(Text) returns the number of characters, spaces included. LENGTH("Happy Days") is 10.
  • SUBSTRING(Text, Start, Length) returns Length characters, starting at position Start. SUBSTRING("Happy Days", 1, 5) is "Happy".
  • UCASE(Text) returns it in upper case, and LCASE(Text) in lower case. Both work on a STRING or a CHAR.
The phrase Happy Days in ten boxes, one character each, with the space shown as a small open box. Above the boxes are the pseudocode positions 1 to 10, below them the Python indexes 0 to 9. Boxes 7 to 10, holding D, a, y and s, are shaded green, with the label SUBSTRING("Happy Days", 7, 4) = "Days".

The picture shows the positions. Position 6 holds the space. SUBSTRING("Happy Days", 7, 4) starts at the D and takes 4 characters: "Days". Python counts from 0, so the same piece is phrase[6:10].

pseudocode
DECLARE Phrase : STRING
Phrase ← "Happy Days"
OUTPUT LENGTH(Phrase)
OUTPUT SUBSTRING(Phrase, 7, 4)
OUTPUT UCASE(Phrase)
OUTPUT LCASE(SUBSTRING(Phrase, 1, 1))

The outputs are 10, Days, HAPPY DAYS and h.

A loop over a string

To look at each character in turn, loop from 1 to LENGTH(...) and take a substring of length 1. This program counts the vowels:

pseudocode
DECLARE Phrase : STRING
DECLARE Letter : CHAR
DECLARE Position : INTEGER
DECLARE Vowels : INTEGER
Phrase ← "Happy Days"
Vowels ← 0
FOR Position ← 1 TO LENGTH(Phrase)
    Letter ← LCASE(SUBSTRING(Phrase, Position, 1))
    CASE OF Letter
      'a' : Vowels ← Vowels + 1
      'e' : Vowels ← Vowels + 1
      'i' : Vowels ← Vowels + 1
      'o' : Vowels ← Vowels + 1
      'u' : Vowels ← Vowels + 1
    ENDCASE
NEXT Position
OUTPUT "Vowels: ", Vowels

LCASE turns each character to lower case, so a capital vowel counts too. A CASE statement with no OTHERWISE does nothing for the other characters. The output is Vowels: 2, for the a of Happy and the a of Days. The y is not a vowel here.

Practise with new numbers

Try it in Python

The program writes SUBSTRING as a small Python function, then uses it to count vowels. It also shows DIV, MOD, ROUND and RANDOM.

Read it first. Then run it. Run it twice: which line changes?

1. Change the phrase to "Computer Science". Predict the length and the number of vowels first.

2. Change the 6 in the last part to 1. Which whole numbers can appear now?

Common mistakes

  • Counting positions from 0 when the question uses 1. Read the question.
  • Forgetting the space. A space is a character. It counts in LENGTH and takes a position.
  • SUBSTRING running past the end. SUBSTRING("Days", 3, 4) asks for characters that are not there.
  • Swapping the start and the length. The second value is the start, the third is how many.
  • Thinking RANDOM gives a whole number. It gives a number between 0 and 1. Multiply and round to get whole numbers.

Remember these

  • DIV whole part, MOD remainder, ROUND(Value, Places), RANDOM() from 0 to 1.
  • LENGTH, SUBSTRING(Text, Start, Length), UCASE, LCASE.
  • The first character is usually at position 1 in pseudocode, and at 0 in Python.

Check yourself

Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.

  1. What does SUBSTRING("Notebook", 5, 4) return?
  1. What does LENGTH("A to Z") return?
  1. Write a condition that is TRUE when Number is odd.
  1. What does ROUND(2.718, 1) return?
  1. Which values can ROUND(RANDOM() * 3, 0) give?

Enroll free in Procedures, Functions and Strings in Pseudocode

Every lesson is open to read. Enrolling also lets you:

  • Mark lessons done and carry on where you stopped.
  • Count the course towards the certificate for its chapter.
No payment. You can leave at any time.

Problems (4)

Follow the string routines. Write down the three lines of output.Easy · High School (9–10) · by StemPy Lab
This program looks at each character of a sentence…Medium · High School (9–10) · by StemPy Lab
This program finds the average of three marks…Medium · High School (9–10) · by StemPy Lab
Next: lesson 3Maintainable programs and the long scenario question
← Procedures, Functions and Strings in Pseudocode
← Procedures, functions and parametersMaintainable programs and the long scenario question →

Discussion (0)

Log in to join the discussion.

Loading…

1 Python program
5 questions to check yourself

This lesson is free to read. Enroll free to keep your place in the course.

A year is a leap year when it divides exactly by 4, except that a year dividing by 100 is a leap…Hard · High School (9–10) · by StemPy Lab

Python Labs (1)

SUBSTRING and a letter countPython Lab · by StemPy Lab