0
0
Pythonprogramming~10 mins

String slicing behavior in Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to get the first three characters of the string.

Python
text = "hello"
slice = text[[1]]
print(slice)
Drag options to blanks, or click blank then click option'
A0:3
B1:4
C3:5
D2:5
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using 1:4 instead of 0:3 to get the first three characters.
Forgetting that the end index is not included in slicing.
2fill in blank
medium

Complete the code to get the last two characters of the string.

Python
text = "world"
slice = text[[1]]
print(slice)
Drag options to blanks, or click blank then click option'
A-3:-1
B3:5
C2:4
D-2:
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using 3:5 works but is less flexible for strings of different lengths.
Using -3:-1 misses the last character.
3fill in blank
hard

Fix the error in the code to reverse the string using slicing.

Python
text = "python"
reversed_text = text[[1]]
print(reversed_text)
Drag options to blanks, or click blank then click option'
A::-1
B1::-1
C::-2
D1:0:-1
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using 1::-1 only reverses part of the string.
Using ::-2 skips characters and does not fully reverse.
4fill in blank
hard

Fill both blanks to get every second character from index 1 to 6.

Python
text = "abcdefghij"
slice = text[[1]:[2]:2]
print(slice)
Drag options to blanks, or click blank then click option'
A1
B6
C5
D7
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using 1:5 misses the character at index 5.
Using 1:7 includes an extra character beyond index 6.
5fill in blank
hard

Fill all three blanks to extract characters from index 2 to 8 skipping every 3rd character.

Python
text = "abcdefghijklm"
slice = text[[1]:[2]:[3]]
print(slice)
Drag options to blanks, or click blank then click option'
A2
B8
C3
D5
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using step 5 skips too many characters.
Using stop index 5 cuts the slice too short.