Bird
Raised Fist0
Data Analysis Pythondata~20 mins

Text cleaning pipeline in Data Analysis Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Text Cleaning Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of basic text cleaning with lowercasing and punctuation removal
What is the output of this Python code that cleans a text by making it lowercase and removing punctuation?
Data Analysis Python
import string
text = "Hello, World! Welcome to Data Science."
cleaned = ''.join(ch for ch in text.lower() if ch not in string.punctuation)
print(cleaned)
Ahello world welcome to data science
BHello World Welcome to Data Science
Chello, world! welcome to data science.
DHELLO WORLD WELCOME TO DATA SCIENCE
Attempts:
2 left
💡 Hint
Think about what happens when you convert text to lowercase and remove punctuation characters.
❓ data_output
intermediate
2:00remaining
Result of tokenizing and removing stopwords from text
Given the following code that tokenizes text and removes common stopwords, what is the resulting list?
Data Analysis Python
text = "Data science is fun and exciting"
stopwords = {'is', 'and'}
tokens = text.lower().split()
filtered = [word for word in tokens if word not in stopwords]
print(filtered)
A['data', 'science', 'is', 'fun', 'and', 'exciting']
B['Data', 'science', 'fun', 'exciting']
C['data', 'science', 'fun', 'exciting']
D['data', 'science', 'fun', 'and', 'exciting']
Attempts:
2 left
💡 Hint
Stopwords are removed after converting all words to lowercase.
🔧 Debug
advanced
2:00remaining
Identify the error in this text cleaning function
What error does this code raise when trying to clean text by removing digits and extra spaces?
Data Analysis Python
def clean_text(text):
    import re
    text = re.sub(r'\d+', '', text)
    text = re.sub(r'\s+', ' ', text).strip()
    return text

print(clean_text('Data 123 science 456'))
ANo error, output: 'Data science'
BSyntaxError: invalid syntax
CAttributeError: 'str' object has no attribute 'sub'
DTypeError: 'builtin_function_or_method' object is not callable
Attempts:
2 left
💡 Hint
Check how the strip method is used in the code.
❓ visualization
advanced
2:00remaining
Visualizing word frequency after cleaning text
Which option shows the correct bar chart output for the word frequencies after cleaning and tokenizing the text?
Data Analysis Python
import matplotlib.pyplot as plt
from collections import Counter
text = "Data science is fun. Data science is exciting."
words = [w.lower().strip('.!') for w in text.split()]
counter = Counter(words)
plt.bar(counter.keys(), counter.values())
plt.show()
ABar chart with words ['data', 'science', 'is', 'fun', 'exciting'] and counts [1, 1, 1, 1, 1]
BBar chart with words ['data', 'science', 'is', 'fun', 'exciting'] and counts [2, 2, 2, 1, 1]
CBar chart with words ['Data', 'science', 'is', 'fun', 'exciting'] and counts [2, 2, 2, 1, 1]
DBar chart with words ['data', 'science', 'fun', 'exciting'] and counts [2, 2, 1, 1]
Attempts:
2 left
💡 Hint
Check how words are lowercased and punctuation is stripped before counting.
🚀 Application
expert
3:00remaining
Predict the output of a complex text cleaning pipeline
Given this pipeline that removes URLs, lowercases, removes stopwords, and lemmatizes, what is the final list of words?
Data Analysis Python
import re
from nltk.stem import WordNetLemmatizer
text = "Visit https://example.com for data science tutorials! Data scientists love data."
text = re.sub(r'https?://\S+', '', text)
text = text.lower()
stopwords = {'for', 'is', 'the', 'and', 'a', 'of'}
words = text.split()
words = [w for w in words if w not in stopwords]
lemmatizer = WordNetLemmatizer()
words = [lemmatizer.lemmatize(w) for w in words]
print(words)
A['visit', 'data', 'science', 'tutorial', 'data', 'scientist', 'love', 'data']
B['visit', 'data', 'science', 'tutorials', 'data', 'scientists', 'love', 'data']
C['visit', 'data', 'science', 'tutorials!', 'data', 'scientist', 'love', 'data.']
D['visit', 'data', 'science', 'tutorial', 'data', 'scientists', 'love', 'data']
Attempts:
2 left
💡 Hint
Remember lemmatizer converts plurals to singular and removes punctuation only if stripped before lemmatization.

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