In the modify tasks, students adapt the example code to start to create code of their own.
Make sure that the students add comments to explain what the code does.
A substring returns a sequence of characters from a string. Python uses slices to do this.
Slices treat strings like lists. Each character is given an index number starting with 0.
[0,1,2,3,4,...]
The slice uses two indexes, the start character and the end character (NOTE the end character is NOT INCLUDED in the slice).
The slice indexes come in brackets after the string or the variable containing the string.
# Outputs 'ompu'
print("Computing"[1:5])
word = "dalmatian"
# Outputs 'ma'
print(word[3:5])
Help! My Code Does Not Work!
Make sure to check your code for the following things:
- The start and end indices are surrounded by square brackets
- The start and end indices have a colon
:
between them.
- The end index is one more than the character you want to stop on.