A timetable friendly name.
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:
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.
The new commands used in this program and others that may be useful. Select them below to learn more:
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 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 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 is assigned to be the string w with all instances of string y replaced with string z.
x is assigned to be a string concatenated from all elements of list z separated by string y.
x is assigned to be a new list from string y separated by character 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 is assigned to be the string y, z times. E.g.
x = "@" * 5
Would result in x = "@@@@@"
Questions to think about with this program to check your understanding:
What is the purpose of line 9:
space = teacher.find(" ")
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.
What is the reason for using `space + 1` and `space + 3` in line 10?
`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.
Change the program so that:
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
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.
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