0
0
Pythonprogramming~10 mins

Common string transformations 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 convert the string to uppercase.

Python
text = "hello world"
result = text.[1]()
print(result)
Drag options to blanks, or click blank then click option'
Alower
Bcapitalize
Ctitle
Dupper
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using lower() which converts to lowercase instead.
Using capitalize() which only changes the first letter.
2fill in blank
medium

Complete the code to remove whitespace from both ends of the string.

Python
text = "  hello world  "
clean_text = text.[1]()
print(clean_text)
Drag options to blanks, or click blank then click option'
Astrip
Brstrip
Clstrip
Dtrim
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using rstrip() which removes spaces only from the right.
Using lstrip() which removes spaces only from the left.
Using trim() which is not a valid Python string method.
3fill in blank
hard

Fix the error in the code to replace all spaces with underscores.

Python
text = "hello world"
new_text = text.[1](" ", "_")
print(new_text)
Drag options to blanks, or click blank then click option'
Aswitch
Breplace
Cchange
Dsubstitute
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using substitute() which is not a string method.
Using switch() or change() which do not exist for strings.
4fill in blank
hard

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

Python
words = ["apple", "bat", "car", "dolphin"]
lengths = {word: [1] for word in words if len(word) [2] 3}
print(lengths)
Drag options to blanks, or click blank then click option'
Alen(word)
Bword
C>
D<=
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using the word itself as the value instead of its length.
Using '<=' instead of '>' causing wrong filtering.
5fill in blank
hard

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

Python
words = ["apple", "bat", "car", "dolphin"]
result = { [1]: [2] for word in words if len(word) [3] 4 }
print(result)
Drag options to blanks, or click blank then click option'
Aword.upper()
Blen(word)
C>
Dword
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using the word itself as key instead of uppercase.
Using '<' instead of '>' causing wrong filtering.