Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to get the first character of the string.
Python
word = "hello" first_char = word[[1]] print(first_char)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using 1 instead of 0 for the first character.
Using -1 which gives the last character.
โ Incorrect
The first character of a string is at index 0.
2fill in blank
mediumComplete the code to get the last character of the string using negative indexing.
Python
word = "world" last_char = word[[1]] print(last_char)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using 5 which is out of range.
Using 1 which gives the second character.
โ Incorrect
Negative index -1 gives the last character of the string.
3fill in blank
hardFix the error in the code to get the second last character using negative indexing.
Python
word = "python" second_last = word[[1]] print(second_last)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using 2 which gives the third character.
Using -1 which gives the last character.
โ Incorrect
Index -2 gives the second last character in the string.
4fill in blank
hardFill both blanks to get the middle character of the string 'abcde'.
Python
word = "abcde" middle_char = word[[1] + [2]] print(middle_char)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using 0 + 2 which also equals 2 but not the intended answer.
Using negative indexes here causes confusion.
โ Incorrect
The middle character is at index 2. Using 1 + 1 equals 2.
5fill in blank
hardFill all three blanks to create a dictionary with keys as characters and values as the negation of their position indexes in the string.
Python
word = "code" index_map = { [1]: [2] for [3] in range(len(word)) } print(index_map)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using 'word' instead of 'word[i]' for keys.
Using 'i' instead of '-i' for values.
โ Incorrect
This creates a dictionary where each character maps to the negation of its position index.