Teacher code

Teacher code

A timetable friendly name.

Try


Select the button below to open the Python program in a new window. Run the program and read the lines of code to see if you can understand how it works. It will be helpful to arrange your display so that you can have this browser window on one side of the screen and the code on the other.

Watch this video to learn about the new concepts shown in the program:

Knowledge Organiser

Strings are zero indexed arrays of characters. Therefore you can index them like this:


word = "Hello"

print(word[1])


This code would output the letter "e". The number 1 can be replaced with a variable. Be careful not to reference an index longer than the number of characters in the string minus one.

Command summary

The new commands used in this program and others that may be useful. Select them below to learn more:

x = y.find(z)

x is assigned the index of the first instance of string z in string y. Returns -1 if the string is not found. Alternatively you can use:

x = y.index(z)

But this will return an error if the string is not found.

x = w[y: z]

x is assigned to be the substring from index y to index z - 1 from string w. E.g. `"Hello"[2:5]` is "llo". This is useful for extracting a string from the beginning or middle of another string.

x = y[-z:]

x is assigned to be the substring in string y starting at the end and working backwards z characters. This is useful for extracting a string from the end of another string.

x = w.replace(y, z)

x is assigned to be the string w with all instances of string y replaced with string z.

x = y.join(z)

x is assigned to be a string concatenated from all elements of list z separated by string y.

x = y.split(z)

x is assigned to be a new list from string y separated by character z.

x = y.strip(z)

x is assigned to be string y with all leading and trailing optional characters z removed.

x = x.strip()

Would remove any white space or escape characters at the beginning or end of string x.

x = y * z

x is assigned to be the string y, z times. E.g.

x = "@" * 5

Would result in x = "@@@@@"

Investigate


Questions to think about with this program to check your understanding:

Purpose question

What is the purpose of line 9:

space = teacher.find(" ")

REVEAL ANSWER

To find the index of where the space is in the teacher string. You need to know where the space is so that you can extract the next two letters.

Reason question

What is the reason for using `space + 1` and `space + 3` in line 10?

REVEAL ANSWER

`space + 1` is the index of the first character after the space. `space + 3` means up to two more characters after the space. The last index is always one more than you think it should be.

Make


Success Criteria

Change the program so that:

  1. The user can enter the number of letters to output.
  2. An apostrophe in the name is removed.

Typical inputs and outputs from the program would be:

Enter the name of the teacher: john smith

How many letters: 3

JSM


Enter the name of the teacher: john smith

How many letters: 5

JSMIT


Enter the name of the teacher: Dave O'Loughlin

How many letters: 4

DOLO

Help

Use the flowcharts if you need more support in understanding the steps required to meet the criteria. Use the Parsons code sorting exercise only if you are really stuck.

Evaluate


Run your code to check that your program has met the success criteria and produces the required output.

Enter the name of the teacher: Craig Sargent

How many letters: 3

CSA

Enter the name of the teacher: Craig Sargent

How many letters: 5

CSARG

Enter the name of the teacher: Dave Hillyard

How many letters: 2

DH

Enter the name of the teacher: Dave Hillyard

How many letters: 4

DHIL

Enter the name of the teacher: Sam O'Toole

How many letters: 3

SOT

Registered in England and Wales: 10442992

VAT Number: 290 9845 58

Telephone: 01452 947500

Email: admin@craigndave.co.uk

Craig'n'Dave logo

Craig 'n' Dave

Bett Awards 2024 Finalist