String Manipulation – Lesson Code

timetable

Learning Objectives


In this post we will focus on string manipulation techniques using five common techniques used in most procedural programming languages:

  • LEFT: to extract characters at the beginning of a string,

  • RIGHT: to extract characters at the end of a string,

  • LENGTH: to find out the number of characters in a string,

  • MID: to extract a set number of characters at a given position in a string,

  • LOCATE: to find if a substring is included in another string and if so return the position of this substring in the string.

LEFTRIGHTLENGTHMIDLOCATE
e.g. LEFT(myString, 5)

>>> to extract the first 5 characters of a string.

e.g. RIGHT(myString, 5)

>>> to extract the last 5 characters of a string.

e.g. LENGTH(myString)

>>> to retrieve the number of characters in a string.

e.g. MID(myString, 3, 5)

>>> to extract a set number of characters at a given position in a string.

e.g. LOCATE(“World”, “Hello World”)

>>> to find if a substring is included in another string and if so return the position of this substring in the string.
If the substring is not part of the string, the value -1 is returned.

Your Challenge


A timetabling system used by school stores information about each lesson such as the day and period of the lesson, the subject, the class teacher, and the room number. The system enables us to extract all these details in the following format:

Year Group-Subject-Day of the Week-Week Number (1 or 2):Period of the day (1 to 5)-Title (Mr/Mrs)-Teacher Name-Classroom

For examples we have extracted these 4 lessons from the system:

8-ICT-Mo1:3-Mrs.Johnson-A12
9-PE-Tu2:4-Mr.Smith-B8
11-Art-We2:1-Mr.Taylor-C12
13-Hi-Th1:3-Mrs.Fox-A9

Check the code below which is based on the 5 string manipulation functions we mentioned above: Left, Right, Length, Mid, and Locate.

Your challenge is to tweak this code to extract all the information from a lesson code and display it in the following format:

    Lesson Code: 8-ICT-Mo1:3-Mrs.Johnson-A12
    Year Group: 8
    Subject: ICT
    Week Day: Monday
    Week Number: 1
    Period: 3
    Teacher: Mrs Johnson
    Classroom: A12

Share Button
Posted in Computer Science, Python - Intermediate, Python Challenges Tagged with: ,

Our Latest Book