Understand the idea
A problem you can recognise
A student types a subject with spaces on either side, and your screen needs a short label. It is tempting to keep cutting characters until the output looks right. But a label of a different length will expose that guess. Indexing, slicing and string methods give you precise tools for precise jobs.
We will use a short word first so that every position can be checked by hand. Then the same reasoning will explain why cleaning text produces a new value and why a six-character prefix stops at index six without including that position.
Understand the idea
Understand the idea
A string is an immutable sequence of Unicode code points. Indexing chooses one position, beginning at zero. Negative indices count back from the end, so -1 selects the last character of a nonempty string. Immutability means assigning text[0] = "X" does not edit a character in place. Construct a new string when you want changed text.
A slice describes a range using start:stop, where stop is excluded. Leaving start empty begins at the start; leaving stop empty continues to the end. Slicing is forgiving when a boundary extends beyond the string, but indexing a missing position raises IndexError. These are different operations, not inconsistent handling of the same request.
Methods such as strip and lower return new strings. Assign their result when you need to keep it. strip removes leading and trailing whitespace, not every space inside a sentence. Work through a short concrete example before combining several methods. A visible character can contain multiple Unicode code points, so advanced text editing needs more care than assuming every index corresponds to one user-perceived character.
See how it works
Example 1: Read positions
PYTHON exampleRead the code, then try a change.
Try this example
word = "Python"
print(word[0])
print(word[-1])
print(len(word))
Expected output:
ReferenceCompare this with the explanation.
P
n
6
P occupies index zero and n is the last character. len reports the sequence length. These ASCII characters each occupy one code point, making the index model easy to inspect.
See how it works
Example 2: Take a slice
PYTHON exampleRead the code, then try a change.
Try this example
word = "Python"
print(word[1:4])
print(word[:2])
print(word[4:])
Expected output:
ReferenceCompare this with the explanation.
yth
Py
on
The first slice includes indices one, two and three, but not four. The omitted bounds in the next two slices mean start of string and end of string respectively. The original word remains unchanged.
See how it works
Example 3: Keep a method result
PYTHON exampleRead the code, then try a change.
Try this example
label = " Study Time "
clean = label.strip().lower()
print(clean)
print(label.startswith(" "))
Expected output:
ReferenceCompare this with the explanation.
study time
True
The cleaned value is a new string. The original label still starts with spaces. lower changes letter case; strip removes only the surrounding whitespace in this example.
Understand the idea
Think through the important decisions
Draw Python on paper and put an index under each character, beginning with zero. Mark the positions included by [1:4]. There are three selected positions: one, two and three. The stop value describes where selection ends, not the last position included. This convention lets adjacent slices meet at a boundary without sharing a character.
Next compare the original label and the cleaned label in the third example. The method result must be assigned if you want a name to refer to it. Calling strip without keeping its result does not remove spaces from the original string. The example's final check is there to reveal that original state deliberately.
Try an empty string before using a last-character index in your own solution. A slice of an empty string can be empty, while asking for its last character has no valid position to return. Decide which operation expresses your intention instead of treating both as interchangeable ways to get some text.
Check your understanding
Check the reasoning
A slice excludes its stop position and returns a new string.
A matching screenshot or output is one check, not proof that a program is correct for all inputs. Change one input, predict the new result and run again. If the result differs, find the first instruction where your prediction stops matching the program state. Keep that small case as a regression check when you fix the cause.
Spot the mistake
The mistake worth understanding
Do not confuse stripping surrounding whitespace with removing every space. Internal spaces in a person's name or a subject title can be meaningful. Likewise, a visible symbol can contain multiple Unicode code points. Our short ASCII examples explain the indexing model; they do not promise that arbitrary human text can be edited safely by counting visible shapes.
Your turn
Independent practice
Use the string " Python Practice ". Save a cleaned version, print its first six characters and its last character, and verify that the original still contains its surrounding spaces. Explain the excluded stop index.
Your turn
Practice hint
Store strip() in a new name. A six-character prefix uses [:6], not [:5].
Your turn
Try a different case
Clean a label, keep the original, and predict a prefix and suffix before running. Then repeat with a shorter label. Explain which operations tolerate a boundary beyond the available text and which need a real character at that position.