A timetable friendly name.
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 = 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 * zx 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
# Teacher code program
# -------------------------
# Subprograms
# -------------------------
---
def tcode(teacher, number):
---
# Remove apostrophe
---
teacher = teacher.replace("'", "")
---
letters = ["", ""]
---
# Extract first letter
---
letters[0] = teacher[0]
---
space = teacher.find(" ")
---
# Extract number of letters after the space
---
letters[1] = teacher[space + 1: space + number]
---
# Join letters together into a single string
---
teacher_code = "".join(letters)
---
# Convert to upper case
---
teacher_code = teacher_code.upper()
---
return teacher_code
---
# -------------------------
# Main program
# -------------------------
---
teacher = input("Enter the name of the teacher: ")
number = int(input("How many letters: "))
---
teacher_code = tcode(teacher, number)
---
print(teacher_code)