0
0
DSA Pythonprogramming~10 mins

String Traversal and Character Access in DSA Python - Interactive Practice

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

Complete the code to print each character in the string word using a for loop.

DSA Python
word = "hello"
for [1] in word:
    print([1])
Drag options to blanks, or click blank then click option'
Achar
Bi
Cword
Dindex
Attempts:
3 left
💡 Hint
Common Mistakes
Using the string variable name itself in the loop variable.
Using an index variable without accessing characters by index.
2fill in blank
medium

Complete the code to print the character at index 2 of the string word.

DSA Python
word = "world"
print(word[1])
Drag options to blanks, or click blank then click option'
A2
B[1]
C[2]
D[3]
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses instead of square brackets.
Using the wrong index number.
3fill in blank
hard

Fix the error in the code to print all characters of text using a while loop.

DSA Python
text = "data"
i = 0
while i < len(text):
    print(text[1])
    i += 1
Drag options to blanks, or click blank then click option'
A[i]
B(i)
C{i}
Di
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses or braces instead of square brackets for indexing.
Not incrementing the index variable.
4fill in blank
hard

Fill both blanks to create a dictionary that maps each index of word to its character.

DSA Python
word = "code"
index_map = { [1]: [2] for [1] in range(len(word)) }
Drag options to blanks, or click blank then click option'
Ai
Bword[i]
Cchar
Dindex
Attempts:
3 left
💡 Hint
Common Mistakes
Using the character variable as the loop variable instead of the index.
Swapping the key and value in the dictionary comprehension.
5fill in blank
hard

Fill all three blanks to create a list of uppercase characters from word that come after the letter 'a'.

DSA Python
word = "alphabet"
result = [[1].upper() for [2] in word if [3] > 'a']
Drag options to blanks, or click blank then click option'
Achar
Dletter
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in the list comprehension.
Not calling upper() on the character.