Python practice: Text Surgery & Regex with .str Accessor

Chapter 23 · 8 questions · included in full access

  1. Question 1 · easy

    Strip whitespace from customer names

    Load "orders_messy.csv". Strip whitespace using messy["Customer"] = messy["Customer"].str.strip(). Print messy["Customer"].iloc[0]:

    Solve it in your browser
  2. Question 2 · easy

    Title-case normalization

    Load "orders_messy.csv". Title-case cities using messy["City"] = messy["City"].str.strip().str.title(). Print messy["City"].iloc[1]:

    Solve it in your browser
  3. Question 3 · easy

    Filter substring with .str.contains

    Load "orders_messy.csv". Find customers containing "Traders" case-insensitively using messy["Customer"].str.contains("traders", case=False). Print matching row count:

    Solve it in your browser
  4. Question 4 · medium

    Split compound codes into columns

    Given df = pd.DataFrame({"Code": ["INV-2026-001", "SO-2025-002"]}), split with df["Code"].str.split("-", expand=True). Print split_df.iloc[0, 1]:

    Solve it in your browser
  5. Question 5 · medium

    Extract digits using regex .str.extract

    Given df = pd.DataFrame({"Notes": ["Order #891", "Invoice #1402"]}), extract digits with df["Notes"].str.extract(r"#(\d+)"). Print df["ID"].iloc[0]:

    Solve it in your browser
  6. Question 6 · easy

    Replace characters with .str.replace

    Load "orders_messy.csv". Remove hyphens from OrderID using messy["OrderID"].str.replace("-", ""). Print the first cleaned ID:

    Solve it in your browser
  7. Question 7 · easy

    Test prefix with .str.startswith

    Load "orders_messy.csv". Check how many orders start with "SO" using messy["OrderID"].str.startswith("SO").sum(). Print the count:

    Solve it in your browser
  8. Question 8 · easy

    Character length with .str.len

    Load "orders_messy.csv". Clean customer names with .str.strip(), then find max name length using .str.len().max(). Print max length:

    Solve it in your browser

These questions are part of full access.

The first chapters of every track are free. Full access is a one-time payment.

See pricing