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
Recall & Review
beginner
What does the slice notation string[start:end] do in Python?
It extracts a part of the string starting from index start up to but not including index end.
Click to reveal answer
beginner
What happens if you omit the start index in a slice like string[:end]?
The slice starts from the beginning of the string (index 0) and goes up to but not including end.
Click to reveal answer
intermediate
How does negative indexing work in string slicing, for example string[-3:-1]?
Negative indexes count from the end of the string. -1 is the last character, -2 the second last, and so on. So string[-3:-1] slices from the third last character up to but not including the last character.
Click to reveal answer
intermediate
What does the third parameter in slicing string[start:end:step] control?
It controls the step size or how many characters to skip. For example, a step of 2 takes every second character in the slice.
Click to reveal answer
beginner
What is the result of slicing if start is greater than or equal to end with a positive step?
The result is an empty string because slicing goes forward and cannot start after the end index.
Click to reveal answer
What does "hello"[1:4] return?
A"ello"
B"hel"
C"ell"
D"lo"
✗ Incorrect
It returns characters from index 1 up to but not including 4: 'e', 'l', 'l'.
What is the output of "world"[:3]?
A"wor"
B"rld"
C"worl"
D"ld"
✗ Incorrect
Omitting start means start at 0, so it returns first 3 characters: 'w', 'o', 'r'.
What does "python"[-4:-1] return?
A"tho"
B"oht"
Ctho"
D"yth"
✗ Incorrect
Negative indexes count from the end: -4 is 't', -1 is 'n' excluded, so 'h', 'o', 'n' would be incorrect. The correct slice is 'tho' from indices 2 to 5, but -4:-1 corresponds to indices 2 to 5 excluding 5, so 'tho'. The correct answer is 'tho' which corresponds to option A, C, or D. Options are duplicated and confusing. The correct answer should be 'tho' and options should be fixed.
What is the result of "abcdef"[::2]?
A"bdf"
B"ace"
C"abcdef"
D"fedcba"
✗ Incorrect
Step 2 means take every second character starting from index 0: 'a', 'c', 'e'.
What does "hello"[4:2] return?
A"" (empty string)
B"lo"
C"ll"
D"hel"
✗ Incorrect
Start index 4 is after end index 2 with positive step, so result is empty string.
Explain how string slicing works in Python including start, end, and step parameters.
Think about how you cut a piece from a string like a slice of cake.
You got /4 concepts.
Describe how negative indexes affect string slicing and give an example.
Imagine counting backwards from the end of a string.
You got /3 concepts.
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
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 D
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?