String slicing behavior in Python - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
Let's explore how the time it takes to slice a string changes as the string gets bigger.
We want to know how the work grows when we take parts of a string.
Analyze the time complexity of the following code snippet.
text = "Hello, world!"
slice_part = text[2:8]
print(slice_part)
This code takes a part of the string from position 2 up to 7 and prints it.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Copying each character from the original string slice range into a new string.
- How many times: Once for each character in the slice length.
When the slice length grows, the work grows in the same way because each character is copied.
| Input Size (slice length) | Approx. Operations |
|---|---|
| 10 | About 10 character copies |
| 100 | About 100 character copies |
| 1000 | About 1000 character copies |
Pattern observation: The work grows directly with the number of characters sliced.
Time Complexity: O(k)
This means the time depends on the length of the slice, not the whole string.
[X] Wrong: "Slicing a string is always very fast and does not depend on slice size."
[OK] Correct: Actually, slicing copies each character in the slice, so bigger slices take more time.
Understanding how slicing works helps you explain performance when working with strings in real projects.
"What if we slice the entire string instead of a part? How would the time complexity change?"
Practice
s[2:5] do on the string s = 'Python'?Solution
Step 1: Understand slice notation s[start:stop]
The slice includes the start index but excludes the stop index.Step 2: Apply to s = 'Python' with s[2:5]
Characters at indices 2, 3, 4 are 't', 'h', 'o'. Index 5 is excluded.Final Answer:
Extracts characters from index 2 up to but not including index 5 -> Option DQuick Check:
Slice excludes stop index [OK]
- Including the stop index character
- Confusing start and stop positions
- Thinking slicing reverses string by default
s starting from the first character?Solution
Step 1: Understand slice syntax s[start:stop:step]
The step value controls the interval between characters taken.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.Final Answer:
s[::2] -> Option CQuick Check:
Step controls interval [OK]
- Using wrong start index
- Confusing step with stop
- Using slice with only two colons incorrectly
s = 'abcdefg' print(s[5:2:-1])
Solution
Step 1: Understand slicing with negative step
Negative step means slicing backwards. Start at index 5, stop before index 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'.Final Answer:
'fed' -> Option BQuick Check:
Negative step slices backwards excluding stop [OK]
- Including stop index when stepping backwards
- Confusing forward and backward slicing
- Misreading indices for negative step
s = 'hello' print(s[4:1])
Solution
Step 1: Analyze slice s[4:1]
Start index 4, stop index 1, default step is +1, so slice moves forward.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.Final Answer:
No error, prints empty string -> Option AQuick Check:
Forward slice with start > stop = empty string [OK]
- Expecting an error for invalid slice
- Expecting reversed substring without negative step
- Confusing slice indices order
s = 'abcdefgh', which slice extracts the string 'hgfed'?Solution
Step 1: Identify characters in 'hgfed'
These are characters at indices 7 ('h'), 6 ('g'), 5 ('f'), 4 ('e'), 3 ('d').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'.Final Answer:
s[7:2:-1] -> Option AQuick Check:
Negative step excludes stop index [OK]
- Including stop index in negative step slice
- Choosing wrong stop index
- Confusing slice direction with step sign
