A1 - String and List Indexing
A string can be more precisely defined as a sequence of characters, where a character can be any type of letter, number, symbol, or even an emoji 😉 (and a few other special things). Each character in a string is assigned a position or index within the string, starting at 0. For example, consider the string I am a string. The index of each character in the string is shown below:

Python gives us lots of ways to work with string data. Consider the following code:
# Create some strings
str1 = "Eastview"
str2 = " Secondary School"
# Test some basic string functionality
print(str1) # print str1
print(str1[4]) # print the 5th letter in str1
print(str1[0:3]) # print the first 3 letters of str starting at position 0
print(str1 + str2) # concatenate (or join) str1 and str2
To elaborate on the last five lines:
print(str1)- self explanatoryprint(str1[4])- prints the fifth character instr1. It's tempting to assume it will print the fourth character, but since the first character in a string is assigned index0, the second will be assigned index1, the third will be assigned index2, and so on. It's always off by one from what we might expect!print(str1[0:3])- This notation allows us to print a substring, or a snippet of the string. The first number in the square brackets (before the colon) indicates what index of the string we want to start at. The second number (after the colon) indicates the position past where we want to end. In this case, we start at index0and end at index2.print(str1 + str2)- joins these two strings together. This is called concatenating the two strings.
Lists
Python also allows us to store lists of values. We can work with lists very similarly to how we work with strings. Here is an example, with comments describing each section.
# We create a list by putting comma separated values within square brackets
exam_marks = [82, 78, 63, 98, 50, 77]
# Print the full list
print(exam_marks)
# Print the third mark
print(exam_marks[2])
# Print the second through fourth mark
print(exam_marks[1:4])
# Change the second mark to 80, then print the updated list
exam_marks[1] = 80
print(exam_marks)
# Calculate a print the different between the first and last mark
diff = exam_marks[0] - exam_marks[5]
print(diff)
# Lists can also contain other data types, such as strings
toppings = ["Sauce", "Cheese", "Pepperoni", "Mushrooms"]
# Print the second topping
print(toppings[1])
# And finally, a little confusing, but this is how we can access
# the fifth letter of the fourth word.
# It will print the "r" from the word "Mushrooms"
print(toppings[3][4])
Your Task
Run update.bat, which will create a Unit 3 folder for you that will contain
two files you will be completing for this assignment.
Part A
In the Unit-3 folder, open a1a.py, which will contain this code:
Then, add a line of code to do each of the following, making use of the techniques introduced in this assignment.
- Print your first name
- Print the third letter of your first name
- Print the second half of your last name (if your last name has an odd number of letters, start with the middle letter).
- Print your full first and last name using string concatenation. To get a space, you can do something like this:
print(str1 + " " + str2)
Part B
In the Unit-3 folder, open a1b.py.
In this file:
- Create a list containing five numbers of your choice
- Calculate the average of the five numbers. As a reminder, to average five numbers, you add them all up then divide by five.
When calculating, do not retype the specific numbers from your list, but instead access each list position.
The start of your calculation should look something like this:
average = (nums[0] + nums[1] +... - Print the average.