Python practice: Text Surgery & Regex with .str Accessor
Chapter 23 · 8 questions · included in full access
Question 1 · easy
Strip whitespace from customer names
Solve it in your browserLoad
"orders_messy.csv". Strip whitespace usingmessy["Customer"] = messy["Customer"].str.strip(). Printmessy["Customer"].iloc[0]:Question 2 · easy
Title-case normalization
Solve it in your browserLoad
"orders_messy.csv". Title-case cities usingmessy["City"] = messy["City"].str.strip().str.title(). Printmessy["City"].iloc[1]:Question 3 · easy
Filter substring with .str.contains
Solve it in your browserLoad
"orders_messy.csv". Find customers containing"Traders"case-insensitively usingmessy["Customer"].str.contains("traders", case=False). Print matching row count:Question 4 · medium
Split compound codes into columns
Solve it in your browserGiven
df = pd.DataFrame({"Code": ["INV-2026-001", "SO-2025-002"]}), split withdf["Code"].str.split("-", expand=True). Printsplit_df.iloc[0, 1]:Question 5 · medium
Extract digits using regex .str.extract
Solve it in your browserGiven
df = pd.DataFrame({"Notes": ["Order #891", "Invoice #1402"]}), extract digits withdf["Notes"].str.extract(r"#(\d+)"). Printdf["ID"].iloc[0]:Question 6 · easy
Replace characters with .str.replace
Solve it in your browserLoad
"orders_messy.csv". Remove hyphens fromOrderIDusingmessy["OrderID"].str.replace("-", ""). Print the first cleaned ID:Question 7 · easy
Test prefix with .str.startswith
Solve it in your browserLoad
"orders_messy.csv". Check how many orders start with"SO"usingmessy["OrderID"].str.startswith("SO").sum(). Print the count:Question 8 · easy
Character length with .str.len
Solve it in your browserLoad
"orders_messy.csv". Clean customer names with.str.strip(), then find max name length using.str.len().max(). Print max length:
These questions are part of full access.
The first chapters of every track are free. Full access is a one-time payment.
See pricing