Strings & Text Processing — Lesson 2: Take a slice of a string
New skill: Slicing, bounds and uppercasing
Previously learned: Units 01–06
Why this matters
Apps create short category tags, previews, and IDs from longer names.
Step 1 — Explore working Python
Read the program below. Before running it, write down the result you expect.
word = 'playground'
print(word[:4])
print(word[4:])
print(word[:4].upper())
Step 2 — Trace the code
- Read
word[:4]as characters from the beginning up to, but not including, index 4. - Change the ending index and compare the outputs.
- Apply
.upper()to a slice to create an uppercase code.
Step 3 — Check your understanding
Predict 'coding'[1:4]. Which letters does Python include?
Step 4 — Watch for this mistake
A slice uses an exclusive end position. [:3] takes three characters, not four.
Practice: Take a slice of a string
Complete short_code(text) to return the first three characters of text in uppercase. Inputs contain at least three characters.
Function contract: short_code(text)
Starter file: main.py
Work in the editor, using Run to explore and Check to compare against the published examples. Your submitted code, not the public check, determines the recorded score.
Hint 1 — Start
Use a slice that starts at the beginning.Hint 2 — Build
Remember that the stop index is excluded.Hint 3 — Test
Call the string method `.upper()` on the slice.Reflect
In 3–5 sentences, explain the algorithm you chose, one test case you predicted, and how you would detect an incorrect answer.
Optional reading
For another perspective on this skill, see How to Think Like a Computer Scientist (interactive textbook). This Playground lesson, worked example, and exercise are original; no textbook passages were reproduced.