Challenge - 5 Problems
Whitespace Stripping Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of str.strip() on pandas Series with whitespace
What is the output of the following code?
import pandas as pd s = pd.Series([' apple ', ' banana', 'cherry ', ' date ']) result = s.str.strip() print(result.tolist())
Pandas
import pandas as pd s = pd.Series([' apple ', ' banana', 'cherry ', ' date ']) result = s.str.strip() print(result.tolist())
Attempts:
2 left
💡 Hint
str.strip() removes spaces from both ends of each string in the Series.
✗ Incorrect
The str.strip() method removes leading and trailing whitespace from each string in the pandas Series. So all spaces before and after the words are removed.
❓ data_output
intermediate2:00remaining
Count of strings after stripping whitespace
After stripping whitespace from the Series below, how many strings start with the letter 'b'?
import pandas as pd
s = pd.Series([' apple ', ' banana', 'blueberry ', ' date ', ' berry'])
stripped = s.str.strip()
count_b = stripped.str.startswith('b').sum()
print(count_b)Pandas
import pandas as pd s = pd.Series([' apple ', ' banana', 'blueberry ', ' date ', ' berry']) stripped = s.str.strip() count_b = stripped.str.startswith('b').sum() print(count_b)
Attempts:
2 left
💡 Hint
Check which strings start with 'b' after removing spaces.
✗ Incorrect
After stripping, the strings starting with 'b' are 'banana', 'blueberry', and 'berry', so count is 3.
🔧 Debug
advanced2:00remaining
Identify the error in using str.strip() on a non-string column
What error will this code raise?
import pandas as pd
df = pd.DataFrame({'numbers': [1, 2, 3, 4]})
df['clean'] = df['numbers'].str.strip()Pandas
import pandas as pd df = pd.DataFrame({'numbers': [1, 2, 3, 4]}) df['clean'] = df['numbers'].str.strip()
Attempts:
2 left
💡 Hint
The .str accessor only works on string data.
✗ Incorrect
The .str accessor raises AttributeError if the Series contains non-string values like integers.
🚀 Application
advanced2:00remaining
Using str.strip() to clean a DataFrame column before analysis
You have a DataFrame with a column 'city' containing city names with extra spaces. Which code snippet correctly cleans the 'city' column by removing leading and trailing spaces?
import pandas as pd
df = pd.DataFrame({'city': [' New York ', 'Los Angeles ', ' Chicago', 'Houston ']})Pandas
import pandas as pd df = pd.DataFrame({'city': [' New York ', 'Los Angeles ', ' Chicago', 'Houston ']})
Attempts:
2 left
💡 Hint
Use the pandas string accessor for element-wise string operations.
✗ Incorrect
Option C uses the correct pandas .str accessor to apply strip to each string in the Series. Option C tries to call strip on the whole Series, which is invalid. Option C calls strip() without a function reference. Option C passes str.strip but map expects a function without parentheses, so it would work but is less idiomatic.
🧠 Conceptual
expert2:00remaining
Understanding str.strip() behavior with custom characters
What will be the output of this code?
import pandas as pd
s = pd.Series(['xxapplexx', 'xbanana', 'cherryxx', 'xxdate'])
result = s.str.strip('x')
print(result.tolist())Pandas
import pandas as pd s = pd.Series(['xxapplexx', 'xbanana', 'cherryxx', 'xxdate']) result = s.str.strip('x') print(result.tolist())
Attempts:
2 left
💡 Hint
str.strip(chars) removes all characters in 'chars' from both ends until a different character is found.
✗ Incorrect
The str.strip('x') removes all 'x' characters from the start and end of each string. So 'xxapplexx' becomes 'apple', 'xbanana' becomes 'banana', 'cherryxx' becomes 'cherry', and 'xxdate' becomes 'date'.