Bird
Raised Fist0
Data Analysis Pythondata~10 mins

Text cleaning pipeline in Data Analysis Python - Interactive Code Practice

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

Complete the code to convert all text in the list to lowercase.

Data Analysis Python
texts = ['Hello World', 'Data Science']
lower_texts = [text.[1]() for text in texts]
print(lower_texts)
Drag options to blanks, or click blank then click option'
Aupper
Blower
Ctitle
Dcapitalize
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'upper()' instead of 'lower()' changes text to uppercase.
Using 'capitalize()' only changes the first letter.
2fill in blank
medium

Complete the code to remove leading and trailing spaces from each string.

Data Analysis Python
texts = ['  data ', ' science  ']
clean_texts = [text.[1]() for text in texts]
print(clean_texts)
Drag options to blanks, or click blank then click option'
Ajoin
Bsplit
Creplace
Dstrip
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'split()' splits the string into words, not removing spaces.
Using 'replace()' needs extra arguments to remove spaces.
3fill in blank
hard

Fix the error in the code to remove punctuation from the text using string.punctuation.

Data Analysis Python
import string
text = 'Hello, world!'
clean_text = ''.join(char for char in text if char not in [1])
print(clean_text)
Drag options to blanks, or click blank then click option'
Astring.punct
Bstring.punctuations
Cstring.punctuation
Dpunctuation.string
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'string.punct' which does not exist.
Swapping the order like 'punctuation.string' which is invalid.
4fill in blank
hard

Fill both blanks to create a dictionary of word lengths for words longer than 3 characters.

Data Analysis Python
words = ['data', 'is', 'fun', 'science']
lengths = {word: [1] for word in words if [2]
print(lengths)
Drag options to blanks, or click blank then click option'
Alen(word)
Bword
Clen(word) > 3
Dword > 3
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself as the value instead of its length.
Comparing the word string directly to a number.
5fill in blank
hard

Fill all three blanks to create a dictionary of uppercase words and their lengths for words longer than 4 characters.

Data Analysis Python
words = ['hello', 'data', 'science', 'fun']
result = { [1]: [2] for w in words if [3] }
print(result)
Drag options to blanks, or click blank then click option'
Aw.upper()
Blen(w)
Clen(w) > 4
Dw.lower()
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase instead of uppercase for keys.
Not filtering words by length.

Practice

(1/5)
1. What is the main purpose of a text cleaning pipeline in data analysis?
easy
A. To translate text into another language
B. To make text uniform and easier to analyze
C. To increase the length of the text
D. To add more punctuation to the text

Solution

  1. Step 1: Understand text cleaning goal

    Text cleaning aims to prepare text data by making it consistent and easier to work with.
  2. Step 2: Identify correct purpose

    Adding punctuation or increasing length does not help analysis; uniformity does.
  3. Final Answer:

    To make text uniform and easier to analyze -> Option B
  4. Quick Check:

    Text cleaning = uniform text [OK]
Hint: Text cleaning makes text consistent for analysis [OK]
Common Mistakes:
  • Thinking cleaning adds punctuation
  • Confusing cleaning with translation
  • Believing cleaning increases text length
2. Which of the following Python code snippets correctly converts a string text to lowercase?
easy
A. text.lower()
B. lower(text)
C. text.toLowerCase()
D. text.lowercase()

Solution

  1. Step 1: Recall Python string method for lowercase

    Python strings have a method called lower() to convert text to lowercase.
  2. Step 2: Check each option

    text.lower() uses correct syntax. Options B, C, D are invalid in Python.
  3. Final Answer:

    text.lower() -> Option A
  4. Quick Check:

    Python lowercase method = lower() [OK]
Hint: Use text.lower() to lowercase strings in Python [OK]
Common Mistakes:
  • Using JavaScript method toLowerCase() in Python
  • Trying a non-existent function lower() outside string
  • Using incorrect method name like lowercase()
3. Given the following Python code, what is the output of cleaned_text?
import string
text = " Hello, World!  "
cleaned_text = text.lower().strip().translate(str.maketrans('', '', string.punctuation))
print(cleaned_text)
medium
A. " hello world "
B. "hello, world!"
C. "hello world"
D. "HELLO WORLD"

Solution

  1. Step 1: Apply lower() and strip()

    The text becomes "hello, world!" after lowering and removing spaces at ends.
  2. Step 2: Remove punctuation with translate()

    All punctuation marks like comma and exclamation are removed, resulting in "hello world".
  3. Final Answer:

    "hello world" -> Option C
  4. Quick Check:

    Lowercase + strip + remove punctuation = "hello world" [OK]
Hint: Chain methods: lower, strip, then remove punctuation [OK]
Common Mistakes:
  • Forgetting to strip spaces
  • Not removing punctuation correctly
  • Confusing case conversion order
4. Identify the error in this text cleaning function and choose the fix:
def clean_text(text):
    text = text.lower()
    text = text.strip
    return text

print(clean_text("  DATA Science  "))
medium
A. Change text.strip to text.strip()
B. Change text.lower() to lower(text)
C. Add text = text.replace() to remove spaces
D. Remove the return statement

Solution

  1. Step 1: Check method usage

    strip is a method and must be called with parentheses ().
  2. Step 2: Identify fix

    Changing text.strip to text.strip() correctly removes spaces.
  3. Final Answer:

    Change text.strip to text.strip() -> Option A
  4. Quick Check:

    Method calls need parentheses [OK]
Hint: Always call string methods with parentheses [OK]
Common Mistakes:
  • Forgetting parentheses on methods
  • Using wrong syntax for lower()
  • Removing return statement by mistake
5. You have a list of sentences with inconsistent spacing and punctuation:
texts = ["Data Science!  ", "  Machine Learning.", "AI, is fun!"]
Which code snippet correctly cleans all texts by making them lowercase, removing punctuation, and trimming spaces?
hard
A. cleaned = [t.strip().lower() for t in texts]
B. cleaned = [t.lower().replace(' ', '') for t in texts]
C. cleaned = [t.translate(str.maketrans('', '', string.punctuation)) for t in texts]
D. import string cleaned = [t.lower().strip().translate(str.maketrans('', '', string.punctuation)) for t in texts]

Solution

  1. Step 1: Understand cleaning steps needed

    We need lowercase, remove punctuation, and trim spaces for each text.
  2. Step 2: Check each option

    import string cleaned = [t.lower().strip().translate(str.maketrans('', '', string.punctuation)) for t in texts] applies all three steps correctly using list comprehension. Others miss one or more steps.
  3. Final Answer:

    import string cleaned = [t.lower().strip().translate(str.maketrans('', '', string.punctuation)) for t in texts] -> Option D
  4. Quick Check:

    All steps in list comprehension = import string cleaned = [t.lower().strip().translate(str.maketrans('', '', string.punctuation)) for t in texts] [OK]
Hint: Use list comprehension chaining all cleaning steps [OK]
Common Mistakes:
  • Missing punctuation removal
  • Not trimming spaces
  • Removing spaces incorrectly with replace
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard