Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the regular expressions module.
NLP
import [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'regex' or 'regexp' which are not standard Python modules.
Trying to import 'text' which is unrelated.
✗ Incorrect
The Python module for regular expressions is called 're'.
2fill in blank
mediumComplete the code to remove all digits from the text using regex substitution.
NLP
clean_text = re.sub(r'[1]', '', text)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '\w' which removes letters and digits, not just digits.
Using '\s' which matches whitespace, not digits.
✗ Incorrect
The pattern '\d' matches any digit character, so replacing it removes digits.
3fill in blank
hardFix the error in the regex pattern to remove punctuation marks from the text.
NLP
clean_text = re.sub(r'[[1]]', '', text)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using patterns that do not include the dash or fail to handle special characters like '-' properly.
Missing some punctuation marks like dash '-' in the pattern.
✗ Incorrect
Option D provides the pattern that safely matches common punctuation (period, comma, exclamation, question mark, dash) inside the character class, using escapes where appropriate (e.g., for dash).
4fill in blank
hardComplete the code to create a regex that removes all whitespace characters including tabs and newlines.
NLP
clean_text = re.sub(r'[1]', '', text)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Replacing with a space ' ' instead of empty string, which does not remove whitespace.
Using '\S' which matches non-whitespace characters.
✗ Incorrect
The pattern '\s' matches any whitespace character, and replacing it with an empty string '' removes all whitespace.
5fill in blank
hardFill all three blanks to create a dictionary comprehension that maps words to their lengths, but only for words longer than 3 characters.
NLP
lengths = { [1]: [2] for [3] in words if len([3]) > 3 } Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently.
Mapping keys or values incorrectly.
✗ Incorrect
The comprehension uses 'word' as the variable, maps 'word' to its length using len(word), and iterates over 'words'.