format_response from yesterday gave you clean labels for fields with consistent values. But free-text responses from Qualtrics arrive in every shape — " Economics ", "ECONOMICS", "Econ omics". Try calling format_response("Major", " Economics ") and what do you get?
"Major: Economics " — the spaces end up in the label. That breaks my methodology table formatting.
Exactly the problem. Python strings have built-in cleaning methods that chain together. strip() removes leading and trailing whitespace. lower() makes everything lowercase. replace(" ", "_") converts internal spaces to underscores — standard for survey field values:
raw = " Econ Omics "
cleaned = raw.strip().lower().replace(" ", "_")
print(cleaned) # econ_omicsThey chain — so strip() runs first, then lower() on its result, then replace()? It reads like a sentence going left to right.
Left to right, exactly. Each method returns a new string. The next method runs on that result. Think of it as your three-step survey-cleaning protocol: trim the edges, normalise the case, standardise the spaces:
def clean_response_text(raw: str) -> str:
cleaned = raw.strip().lower().replace(" ", "_")
print(f"Cleaned: {cleaned}")
return cleanedSo after running this I can pass the result straight into format_response and the label will always be clean?
That's the composability point — one cleans, one formats. Stack them and messy Qualtrics exports stop being your problem.
I've been doing manual find-and-replace in Excel for every new export. This runs once and handles the whole column.
The order of the chain matters. strip() first — if you lower() before strip(), whitespace is still there. replace() last — it operates on the normalised string. Swap them and you get different output.
Chain methods left to right — each returns a new string, the next method runs on that result.
| Method | Effect | Example |
|---|---|---|
strip() | Remove leading/trailing whitespace | " hi " → "hi" |
lower() | Lowercase all characters | "ECON" → "econ" |
replace(a, b) | Replace every a with b | "econ omics" → "econ_omics" |
strip() first, lower() second, replace() last — each step operates on the output of the previous one. Swap the order and your results change.
format_response from yesterday gave you clean labels for fields with consistent values. But free-text responses from Qualtrics arrive in every shape — " Economics ", "ECONOMICS", "Econ omics". Try calling format_response("Major", " Economics ") and what do you get?
"Major: Economics " — the spaces end up in the label. That breaks my methodology table formatting.
Exactly the problem. Python strings have built-in cleaning methods that chain together. strip() removes leading and trailing whitespace. lower() makes everything lowercase. replace(" ", "_") converts internal spaces to underscores — standard for survey field values:
raw = " Econ Omics "
cleaned = raw.strip().lower().replace(" ", "_")
print(cleaned) # econ_omicsThey chain — so strip() runs first, then lower() on its result, then replace()? It reads like a sentence going left to right.
Left to right, exactly. Each method returns a new string. The next method runs on that result. Think of it as your three-step survey-cleaning protocol: trim the edges, normalise the case, standardise the spaces:
def clean_response_text(raw: str) -> str:
cleaned = raw.strip().lower().replace(" ", "_")
print(f"Cleaned: {cleaned}")
return cleanedSo after running this I can pass the result straight into format_response and the label will always be clean?
That's the composability point — one cleans, one formats. Stack them and messy Qualtrics exports stop being your problem.
I've been doing manual find-and-replace in Excel for every new export. This runs once and handles the whole column.
The order of the chain matters. strip() first — if you lower() before strip(), whitespace is still there. replace() last — it operates on the normalised string. Swap them and you get different output.
Chain methods left to right — each returns a new string, the next method runs on that result.
| Method | Effect | Example |
|---|---|---|
strip() | Remove leading/trailing whitespace | " hi " → "hi" |
lower() | Lowercase all characters | "ECON" → "econ" |
replace(a, b) | Replace every a with b | "econ omics" → "econ_omics" |
strip() first, lower() second, replace() last — each step operates on the output of the previous one. Swap the order and your results change.
You are cleaning the free-text responses from your Qualtrics export before running analysis. Write `clean_response_text(raw)` that strips leading and trailing whitespace, converts to lowercase, and replaces spaces with underscores. For example, `clean_response_text(' Econ Omics ')` should return `'econ_omics'`.
Tap each step for scaffolded hints.
No blank-editor panic.