Challenge - 5 Problems
StrReplace Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of str.replace() with regex in pandas
What is the output of the following code snippet?
Pandas
import pandas as pd df = pd.DataFrame({'text': ['apple123', 'banana456', 'cherry789']}) df['cleaned'] = df['text'].str.replace(r'\d+', '', regex=True) print(df['cleaned'].tolist())
Attempts:
2 left
💡 Hint
The regex '\d+' matches digits and str.replace removes them.
✗ Incorrect
The code removes all digits from each string in the 'text' column using regex '\d+'. The result is the list without numbers.
❓ data_output
intermediate2:00remaining
Resulting DataFrame after str.replace() substitution
Given the DataFrame below, what is the resulting DataFrame after applying the str.replace() method?
Pandas
import pandas as pd df = pd.DataFrame({'city': ['New York', 'Los Angeles', 'San Francisco']}) df['city'] = df['city'].str.replace(' ', '_') print(df)
Attempts:
2 left
💡 Hint
str.replace(' ', '_') replaces spaces with underscores.
✗ Incorrect
The spaces in city names are replaced by underscores, so 'New York' becomes 'New_York', etc.
🔧 Debug
advanced2:00remaining
Identify the error in str.replace() usage
What error will this code raise and why?
Pandas
import pandas as pd df = pd.DataFrame({'code': ['A1', 'B2', 'C3']}) df['code'] = df['code'].str.replace('\d+', '') print(df)
Attempts:
2 left
💡 Hint
Check the parameters passed to str.replace() in pandas.
✗ Incorrect
In pandas, str.replace() requires both 'pat' and 'repl' arguments. Here, repl is missing, causing TypeError.
🚀 Application
advanced2:00remaining
Using str.replace() to clean phone numbers
You have a DataFrame column with phone numbers in various formats. Which code snippet correctly removes all non-digit characters to keep only numbers?
Pandas
import pandas as pd df = pd.DataFrame({'phone': ['(123) 456-7890', '987.654.3210', '+1 800 555 1234']})
Attempts:
2 left
💡 Hint
Use regex to remove all non-digit characters.
✗ Incorrect
The pattern '\D' matches any non-digit character. Replacing them with '' keeps only digits.
🧠 Conceptual
expert2:00remaining
Behavior of str.replace() with overlapping patterns
Consider the string 'aaaa'. What is the result of applying str.replace('aa', 'b') once in pandas with regex=False?
Pandas
import pandas as pd s = pd.Series(['aaaa']) result = s.str.replace('aa', 'b', regex=False) print(result[0])
Attempts:
2 left
💡 Hint
str.replace replaces non-overlapping occurrences from left to right.
✗ Incorrect
The first 'aa' is replaced by 'b', then the next 'aa' is replaced by 'b', resulting in 'bb'.