0
0
Pythonprogramming~10 mins

String indexing (positive and negative) 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 character of the string.

Python
word = "hello"
first_char = word[[1]]
print(first_char)
Drag options to blanks, or click blank then click option'
A1
B0
C-1
D5
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using 1 instead of 0 for the first character.
Using -1 which gives the last character.
2fill in blank
medium

Complete 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'
A-1
B0
C1
D5
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using 5 which is out of range.
Using 1 which gives the second character.
3fill in blank
hard

Fix 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'
A5
B2
C-1
D-2
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using 2 which gives the third character.
Using -1 which gives the last character.
4fill in blank
hard

Fill 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'
A2
B1
C0
D-1
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using 0 + 2 which also equals 2 but not the intended answer.
Using negative indexes here causes confusion.
5fill in blank
hard

Fill 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'
Aword[i]
B-i
Ci
Dword
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using 'word' instead of 'word[i]' for keys.
Using 'i' instead of '-i' for values.