Challenge - 5 Problems
String Cleaning Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this string cleaning code?
Consider the following Python code that cleans a string by stripping spaces, converting to lowercase, and replacing characters. What is the output?
Data Analysis Python
text = ' Hello World! ' cleaned = text.strip().lower().replace('o', '0') print(cleaned)
Attempts:
2 left
💡 Hint
Remember that strip() removes spaces only at the start and end, lower() changes all letters to lowercase, and replace() swaps specified characters.
✗ Incorrect
The strip() removes spaces at both ends, lower() converts all letters to lowercase, and replace('o', '0') changes all 'o' letters to zero '0'. So ' Hello World! ' becomes 'hell0 w0rld!'.
❓ data_output
intermediate2:00remaining
How many unique cleaned strings are in this list?
Given a list of strings, we clean each by stripping spaces and converting to lowercase. How many unique cleaned strings remain?
Data Analysis Python
texts = [' Apple', 'apple ', 'APPLE', 'Banana', ' banana ', 'BANANA '] cleaned_texts = [t.strip().lower() for t in texts] unique_count = len(set(cleaned_texts)) print(unique_count)
Attempts:
2 left
💡 Hint
Think about how strip() and lower() normalize the strings so that different cases and spaces become the same.
✗ Incorrect
After cleaning, all 'Apple' variants become 'apple' and all 'Banana' variants become 'banana'. So only two unique strings remain.
🔧 Debug
advanced2:00remaining
What error does this string cleaning code raise?
This code tries to clean a list of strings but raises an error. What is the error?
Data Analysis Python
texts = ['Data ', None, ' Science'] cleaned = [t.strip().lower() for t in texts] print(cleaned)
Attempts:
2 left
💡 Hint
Check what happens when the code tries to call strip() on None.
✗ Incorrect
None is not a string and does not have the strip() method, so calling t.strip() on None raises AttributeError.
🚀 Application
advanced2:00remaining
Which option produces the cleaned DataFrame with replaced characters?
You have a DataFrame with a column 'Name' containing messy strings. You want to strip spaces, convert to lowercase, and replace 'a' with '@'. Which code produces the correct cleaned DataFrame?
Data Analysis Python
import pandas as pd df = pd.DataFrame({'Name': [' Anna ', 'Mark', ' Sara', 'paul ']})
Attempts:
2 left
💡 Hint
Remember to use pandas string methods with .str for vectorized operations.
✗ Incorrect
Option D correctly chains pandas string methods with .str accessor. Option D fails because strip() and lower() are not methods on Series directly. Option D works but is slower and less idiomatic. Option D is invalid syntax.
🧠 Conceptual
expert2:00remaining
Why is chaining string methods with .str important in pandas?
In pandas, why do we use .str before string methods like strip(), lower(), and replace() when cleaning a Series of strings?
Attempts:
2 left
💡 Hint
Think about how pandas handles operations on columns with many values.
✗ Incorrect
The .str accessor enables vectorized string operations on each element of a pandas Series, making the operations efficient and concise.