Index and slice a label
Quick review (2 minutes)
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.
1 In plain words
A shelf label reads RACK27. To copy it onto a stock card, write from left to right and fit each letter or digit into its own box. Use the box numbers to read back one letter or digit, or several in a row.
Each letter or digit on the label is a character. Python calls a piece of text a string. Write "RACK27" with double quotes to tell Python 3.13 that it is text.
The marks [ and ] are square brackets. They go after the name for the text and hold the numbers that tell Python what to pick. The : mark is a colon. Between two numbers in the brackets, read it as “start at the first number and stop before the second.”
An index is a position number. Remember that Python starts at 0 on the left. Look up where to put the numbers and the colon in The rule.
2 Picture it
Six tiles read R, A, C, K, 2, 7. Their indexes are 0 through 5. The shaded slice covers C, K and 2. A stop line sits before the 7, at index 5.
3 Why
One number inside square brackets picks the character in that box as a string with one character. Two numbers separated by a colon pick a slice, a piece of the string. For the slices here, start at the box named by the first number. If the second number names a box, stop immediately before it. If it equals the character count, stop at the right edge of the last box. The slice is text, even if it contains only digits.
4 The rule
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.
5 How
- 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.
6 Worked example
Read the shelf tag
label = "RACK27"
print(label[1])
print(label[2:5])
Standard input
Empty
Standard output
A
CK2
Type the program in an editor and keep the blank line. Run the file. To check the slice on paper, cover RA before the start and 7 after the stop. The uncovered characters read CK2.
Trace
| Instruction | Output | Explanation |
|---|---|---|
label = "RACK27" | — | Give the whole tag the name label. |
print(label[1]) | A | Index 1 is the second character when the first is at 0. |
print(label[2:5]) | CK2 | Take positions 2, 3 and 4. Subtract 2 from 5 to check the three-character length. |
7 The trap
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.
Quick checks
Quick check 1 of 2
With label holding "RACK27", what text does label[0:2] pick?
Show the steps and the answer
"RA"
Why: Read R at position 0 and A at position 1. Stop before box 2.
Source: Python Software Foundation, Python 3.13 Tutorial, 3.1.2. Text
Quick check 2 of 2
When label holds "MUG84", what do print(label[0]) and print(label[3:5]) display? Which positions does the slice use?
Show the steps and the answer
The first line is M. The second is 84. The slice uses positions 3 and 4.
Why: Give the label MUG84 five boxes. The first print line reads M from box 0. The second reads 8 and 4 from boxes 3 and 4, then stops at the right edge. A slice of a string gives a string.
Source: Python Software Foundation, Python 3.13 Tutorial, 3.1.2. Text
Sources
- Python 3.13 Tutorial, 3.1.2. Text. Python Software Foundation. Python Software Foundation License Version 2. Read 26 Nov 2026.