Bird
Raised Fist0
Pythonprogramming~10 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What does the slice s[2:5] do on the string s = 'Python'?
easy
A. Extracts characters from index 5 to index 2
B. Extracts characters from index 2 to index 5 including both
C. Extracts characters from the start to index 5
D. Extracts characters from index 2 up to but not including index 5

Solution

  1. Step 1: Understand slice notation s[start:stop]

    The slice includes the start index but excludes the stop index.
  2. Step 2: Apply to s = 'Python' with s[2:5]

    Characters at indices 2, 3, 4 are 't', 'h', 'o'. Index 5 is excluded.
  3. Final Answer:

    Extracts characters from index 2 up to but not including index 5 -> Option D
  4. Quick Check:

    Slice excludes stop index [OK]
Hint: Remember stop index is excluded in slicing [OK]
Common Mistakes:
  • Including the stop index character
  • Confusing start and stop positions
  • Thinking slicing reverses string by default
2. Which of the following is the correct syntax to get every second character from string s starting from the first character?
easy
A. s[:2:2]
B. s[2::]
C. s[::2]
D. s[1:2]

Solution

  1. Step 1: Understand slice syntax s[start:stop:step]

    The step value controls the interval between characters taken.
  2. Step 2: Identify correct syntax for every second character from start

    s[::2] means start at 0, go to end, step by 2, so every second character.
  3. Final Answer:

    s[::2] -> Option C
  4. Quick Check:

    Step controls interval [OK]
Hint: Use empty start and stop with step for intervals [OK]
Common Mistakes:
  • Using wrong start index
  • Confusing step with stop
  • Using slice with only two colons incorrectly
3. What is the output of the following code?
s = 'abcdefg'
print(s[5:2:-1])
medium
A. 'cde'
B. 'fed'
C. 'edc'
D. 'gfed'

Solution

  1. Step 1: Understand slicing with negative step

    Negative step means slicing backwards. Start at index 5, stop before index 2.
  2. Step 2: Extract characters from s = 'abcdefg'

    Index 5 is 'f', then 'e' at 4, then 'd' at 3. Stop before 2 means exclude index 2 ('c'). So result is 'fed'.
  3. Final Answer:

    'fed' -> Option B
  4. Quick Check:

    Negative step slices backwards excluding stop [OK]
Hint: Negative step slices backward excluding stop index [OK]
Common Mistakes:
  • Including stop index when stepping backwards
  • Confusing forward and backward slicing
  • Misreading indices for negative step
4. Find the error in this code snippet:
s = 'hello'
print(s[4:1])
medium
A. No error, prints empty string
B. No error, prints 'llo'
C. SyntaxError due to invalid slice
D. RuntimeError due to wrong indices

Solution

  1. Step 1: Analyze slice s[4:1]

    Start index 4, stop index 1, default step is +1, so slice moves forward.
  2. Step 2: Understand slice behavior when start > stop with positive step

    Since start is after stop and step is positive, slice returns empty string without error.
  3. Final Answer:

    No error, prints empty string -> Option A
  4. Quick Check:

    Forward slice with start > stop = empty string [OK]
Hint: Forward slice with start > stop returns empty string [OK]
Common Mistakes:
  • Expecting an error for invalid slice
  • Expecting reversed substring without negative step
  • Confusing slice indices order
5. Given s = 'abcdefgh', which slice extracts the string 'hgfed'?
hard
A. s[7:2:-1]
B. s[7:3:-1]
C. s[7:1:-1]
D. s[7:0:-1]

Solution

  1. Step 1: Identify characters in 'hgfed'

    These are characters at indices 7 ('h'), 6 ('g'), 5 ('f'), 4 ('e'), 3 ('d').
  2. Step 2: Check slice s[7:2:-1]

    Start at index 7 ('h'), move backward to index 3 (stop before 2), so includes indices 7 to 3. This matches 'hgfed'.
  3. Final Answer:

    s[7:2:-1] -> Option A
  4. Quick Check:

    Negative step excludes stop index [OK]
Hint: Negative step excludes stop index, count backward carefully [OK]
Common Mistakes:
  • Including stop index in negative step slice
  • Choosing wrong stop index
  • Confusing slice direction with step sign