0
0
Data Analysis Pythondata~20 mins

String cleaning (strip, lower, replace) in Data Analysis Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
String Cleaning Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A'hell0 w0rld!'
B' hello world! '
C'Hello World!'
D'hello world!'
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.
data_output
intermediate
2: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)
A3
B2
C6
D1
Attempts:
2 left
💡 Hint
Think about how strip() and lower() normalize the strings so that different cases and spaces become the same.
🔧 Debug
advanced
2: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)
AAttributeError: 'NoneType' object has no attribute 'strip'
BTypeError: 'list' object is not callable
CIndexError: list index out of range
DValueError: invalid literal for int() with base 10
Attempts:
2 left
💡 Hint
Check what happens when the code tries to call strip() on None.
🚀 Application
advanced
2: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 ']})
Adf['Cleaned'] = df['Name'].str.strip().lower().replace('a', '@')
Bdf['Cleaned'] = df['Name'].strip().lower().replace('a', '@')
Cdf['Cleaned'] = df['Name'].apply(lambda x: x.strip().lower().replace('a', '@'))
Ddf['Cleaned'] = df['Name'].str.strip().str.lower().str.replace('a', '@')
Attempts:
2 left
💡 Hint
Remember to use pandas string methods with .str for vectorized operations.
🧠 Conceptual
expert
2: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?
ABecause .str is required to convert strings to uppercase only.
BBecause .str converts the Series into a list of strings before applying methods.
CBecause .str allows vectorized string operations on each element of the Series efficiently.
DBecause .str automatically removes missing values before applying string methods.
Attempts:
2 left
💡 Hint
Think about how pandas handles operations on columns with many values.