Fill in

Slice It Up

A slice grabs a whole piece of a string at once: text[start:stop]. It includes start but stops just before stop.

word = "MOONWELL"
print(word[0:4])   # MOON  (indexes 0, 1, 2, 3)
print(word[4:8])   # WELL

Leave a side empty to mean "from the beginning" or "to the end":

print(word[:4])    # MOON
print(word[4:])    # WELL
print(word[-4:])   # WELL  (the last four)

A third number is the step. A step of -1 walks backwards, which reverses the string:

print(word[::-1])  # LLEWNOOM

Slicing never crashes for going past the end. It just stops.

Assignment

Ship codes look like EMB-0427-N. Replace each ___ so that:

  1. port is the first 3 characters (EMB).
  2. number is the 4 digits in the middle (0427).
  3. heading is the last character (N).
  4. backwards is the whole code reversed.