0
0
Data Analysis Pythondata~10 mins

replace() for value substitution in Data Analysis 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 replace all occurrences of 'cat' with 'dog' in the string.

Data Analysis Python
text = 'The cat sat on the cat mat.'
new_text = text.[1]('cat', 'dog')
print(new_text)
Drag options to blanks, or click blank then click option'
Asubstitute
Bswitch
Cchange
Dreplace
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method name that does not exist like 'substitute' or 'change'.
Forgetting to call the method on the string variable.
2fill in blank
medium

Complete the code to replace only the first occurrence of 'apple' with 'orange' in the string.

Data Analysis Python
text = 'apple banana apple grape'
new_text = text.[1]('apple', 'orange', 1)
print(new_text)
Drag options to blanks, or click blank then click option'
Aswap
Breplace
Cchange
Dupdate
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method that does not accept a count argument.
Not passing the count argument to limit replacements.
3fill in blank
hard

Fix the error in the code to replace 'blue' with 'red' in the list of colors.

Data Analysis Python
colors = ['blue', 'green', 'blue']
new_colors = [color.[1]('blue', 'red') for color in colors]
print(new_colors)
Drag options to blanks, or click blank then click option'
Areplace
Bswap
Cchange
Dupdate
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to call replace on the list instead of each string.
Using a method name that does not exist.
4fill in blank
hard

Complete the code to create a dictionary with words as keys and their lengths as values, but only for words longer than 3 letters.

Data Analysis Python
words = ['sun', 'moon', 'star', 'sky']
lengths = {word: [1] for word in words if len(word) > 3}
print(lengths)
Drag options to blanks, or click blank then click option'
A:
Blen(word)
Cword
D=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' instead of ':' in dictionary comprehension.
Using the word itself as the value instead of its length.
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase words as keys and their lengths as values, but only for words longer than 3 letters.

Data Analysis Python
words = ['sun', 'moon', 'star', 'sky']
result = { [1]: [2] for word in words if len(word) [3] 3 }
print(result)
Drag options to blanks, or click blank then click option'
Aword.upper()
Blen(word)
C>
Dword.lower()
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase instead of uppercase for keys.
Using '<' instead of '>' in the condition.
Mixing up keys and values in the comprehension.