0
0
Pythonprogramming~10 mins

String slicing behavior in Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - String slicing behavior
Start with string s
Choose slice indices: start, stop, step
Extract characters from s using indices
Create new substring
Return substring
String slicing takes a part of the string from start index up to but not including stop index, stepping by step.
Execution Sample
Python
s = "Hello, world!"
sub = s[1:8:2]
print(sub)
Extract characters from index 1 to 7 stepping by 2 and print the substring.
Execution Table
StepIndexCharacterIncluded in slice?Substring so far
11eYes"e"
23lYes"el"
35,Yes"el,"
47oYes"el,o"
59rNo"el,o"
611dNo"el,o"
713N/ANo"el,o"
💡 Reached stop index 8, slicing stops before index 8.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
s"Hello, world!""Hello, world!""Hello, world!""Hello, world!""Hello, world!""Hello, world!"
sub"""e""el""el,""el,o""el,o"
Key Moments - 3 Insights
Why does the slice stop before index 8 and not include the character at index 8?
In Python slicing, the stop index is exclusive, so characters are taken up to but not including the stop index, as shown in the execution_table where index 8 is not included.
Why are characters at indices 2, 4, 6 skipped in the slice?
Because the step is 2, the slice takes every second character starting from index 1, skipping indices 2, 4, 6, as shown in the execution_table where only indices 1,3,5,7 are included.
What happens if the step is negative?
A negative step slices the string backwards, starting from start index down to stop index (exclusive), reversing the order of characters.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the substring after step 3?
A"el,"
B"el"
C"e"
D"el,o"
💡 Hint
Check the 'Substring so far' column at step 3 in the execution_table.
At which index does the slicing stop before including the character?
A7
B8
C9
D6
💡 Hint
Look at the exit_note and see which index is exclusive in the slicing.
If the step was changed to 1, how would the substring change?
A"Helo"
B"el,o"
C"ello, w"
D"Hello, world!"
💡 Hint
With step 1, slicing takes every character from start to stop-1, so check indices 1 to 7 in s.
Concept Snapshot
String slicing syntax: s[start:stop:step]
- start: index to begin (default 0)
- stop: index to end (exclusive)
- step: how many indices to jump (default 1)
Returns a new substring from s.
Stop index is not included.
Step can be negative to reverse.
Full Transcript
This visual execution shows how Python string slicing works. We start with a string s and choose start, stop, and step indices. The slice extracts characters from s starting at start index, moving forward by step, and stops before the stop index. For example, s[1:8:2] takes characters at indices 1,3,5,7. The execution table tracks each step, showing which characters are included and the substring built so far. Key points: the stop index is exclusive, and step controls the jump between indices. Negative step slices backwards. This helps beginners see exactly how slicing picks characters and builds the substring.