Index and slice a label — quick review
A shelf label reads RACK27.
With label as the name of the string, label[1] picks the character at index 1. In label[2:5], 2 is the start and 5 is the stop. That takes positions 2, 3, and 4. When the start is smaller than the stop and both numbers are in the range from zero through the total number of characters, subtract the start from the stop to get the slice’s character count.
Number the boxes from left to right, starting at 0.
Give the text the name
labelon the first line. The equals sign=tells Python to use the name on the left for the text on the right.After
label, put the position in square brackets for one character. For a slice, put the start and stop in the brackets with a colon between them.To display the result, put your selection inside the parentheses of
print(). The textprint()displays is the output. For these labels,print()displays the text without the surrounding quotes.In a text editor, type the three nonblank lines under Code in the worked example. Keep the blank line and start each line at the left edge. Run the file with Python 3.13. If you use the Python shell, where you can run one line at a time, type each nonblank source line after the
>>>shown on screen and press Enter.
If you start numbering at 1, you’ll pick R for index 1. Label that first box 0 and the next box 1 to find A. If you include box 5 in the slice, you’ll get CK27. Place your pencil on the line before box 5 and leave the 7 on the far side.