
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:
portis the first 3 characters (EMB).numberis the 4 digits in the middle (0427).headingis the last character (N).backwardsis the whole code reversed.
Ship codes look like EMB-0427-N. Replace each ___ so that:
portis the first 3 characters (EMB).numberis the 4 digits in the middle (0427).headingis the last character (N).backwardsis the whole code reversed.
main.py Python sleeping
Press Run to try your code, then Check to test it.