0
0
Pythonprogramming~10 mins

Iterating over strings 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 print each character in the string.

Python
for char in [1]:
    print(char)
Drag options to blanks, or click blank then click option'
A"hello"
B12345
C['h', 'e', 'l', 'l', 'o']
Dhello
Attempts:
3 left
💡 Hint
Common Mistakes
Not using quotes around the string.
Using a list instead of a string.
2fill in blank
medium

Complete the code to count how many times 'a' appears in the string.

Python
count = 0
for char in "banana":
    if char == [1]:
        count += 1
print(count)
Drag options to blanks, or click blank then click option'
A'b'
Ba
C"a"
D'a'
Attempts:
3 left
💡 Hint
Common Mistakes
Using double quotes instead of single quotes (both work but only one is correct here).
Not using quotes at all.
3fill in blank
hard

Fix the error in the code to print each character in the string.

Python
word = "code"
for [1] in word:
    print(char)
Drag options to blanks, or click blank then click option'
Aword
Bchar
Cletter
Dc
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different variable name in the loop and print statement.
Using the string name as loop variable.
4fill in blank
hard

Fill both blanks to create a dictionary with letters as keys and their uppercase as values.

Python
result = { [1]: [2] for [1] in "abc" }
Drag options to blanks, or click blank then click option'
Achar
Bchar.upper()
Cletter
Dletter.upper()
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names for key and loop variable.
Not calling upper() method correctly.
5fill in blank
hard

Fill all three blanks to create a dictionary with letters as keys, their index as values, but only for letters after 'b'.

Python
result = { [1]: [2] for [1], [2] in enumerate("abcde") if [3] > 1 }
Drag options to blanks, or click blank then click option'
Aletter
Bindex
Didx
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up index and letter variables.
Using wrong variable names in the if condition.