Challenge - 5 Problems
Pattern Matching Pro
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of str.contains() with case sensitivity
What is the output of the following code snippet?
Pandas
import pandas as pd df = pd.DataFrame({'text': ['Apple', 'banana', 'Cherry', 'date']}) result = df['text'].str.contains('a') print(result.tolist())
Attempts:
2 left
💡 Hint
Remember that str.contains() is case sensitive by default.
✗ Incorrect
The pattern 'a' matches only lowercase 'a'. 'Apple' has uppercase 'A' so false, 'banana' true, 'Cherry' false, 'date' true. So the result is [false, true, false, true].
❓ data_output
intermediateFilter rows using str.contains() with regex
Given the DataFrame below, which rows remain after filtering with str.contains('^b.*a$', regex=true)?
Pandas
import pandas as pd df = pd.DataFrame({'words': ['banana', 'beta', 'bat', 'boa', 'baba', 'cab']}) filtered = df[df['words'].str.contains('^b.*a$', regex=True)] print(filtered['words'].tolist())
Attempts:
2 left
💡 Hint
The regex '^b.*a$' means strings starting with 'b' and ending with 'a'.
✗ Incorrect
'banana', 'beta', 'boa', 'baba' start with 'b' and end with 'a'. 'bat' ends with 't', 'cab' starts with 'c'. The output preserves order: ['banana', 'beta', 'boa', 'baba'].
🔧 Debug
advancedIdentify the error in str.contains() usage
What error does the following code raise?
Pandas
import pandas as pd df = pd.DataFrame({'col': ['abc', 'def', 'ghi']}) result = df['col'].str.contains(123) print(result)
Attempts:
2 left
💡 Hint
Check the type of the pattern argument passed to str.contains().
✗ Incorrect
str.contains() expects a string or regex pattern. Passing an integer causes a TypeError.
🚀 Application
advancedUse str.contains() to find rows with multiple patterns
You want to filter rows where the 'text' column contains either 'cat' or 'dog'. Which code correctly does this?
Pandas
import pandas as pd df = pd.DataFrame({'text': ['catfish', 'dogma', 'bird', 'catalog', 'frog']})
Attempts:
2 left
💡 Hint
Use regex OR operator '|' inside the pattern string.
✗ Incorrect
Option B uses regex 'cat|dog' which matches strings containing 'cat' or 'dog'. Other options are invalid or do not work as intended.
🧠 Conceptual
expertUnderstanding na parameter in str.contains()
What is the output of the following code?
Pandas
import pandas as pd import numpy as np df = pd.DataFrame({'text': ['apple', np.nan, 'banana', np.nan]}) result = df['text'].str.contains('a', na=False) print(result.tolist())
Attempts:
2 left
💡 Hint
The na parameter controls the output for missing values.
✗ Incorrect
With na=False, missing values (NaN) are treated as false in the output boolean Series.
