You print out slices of strings which start at index -5
and end at index 4
. Slicing differs from single-element access (x[i]
) not only in one or more colons (:
) being involved, but in so far as you specify a range with a start index and an end index (start is inclusive whereas end isn't). The range [-5:4]
is empty for "university"
and contains two elements for "abcdefg"
.
See for illustration the content of the strings in relation to indices:
0 1 2 3 ]4
u n i v e r s i t y
[-5 -4 -3 -2 -1
0 1 2 3 ]4
a b c d e f g
[-5 -4 -3 -2 -1
... which make it obvious that that the substring for university
must be empty.
What you, probably, might be expecting is a 4-character substring beginning at index -5
which can be achieved by x[-5,-1]
.