Normalize a config value
Problem statement
Config values read from a file or environment variable often come with stray whitespace or inconsistent capitalization: " ENABLED ", "Enabled", "enabled\n" might all be meant to represent the same setting. Comparing them directly with == would treat them as different values, so you normalize the string first: strip the extra whitespace, then lowercase it, so every version becomes the same thing.
Common mistake: normalizing only one of the two steps.
Wrong: value.lower() # spaces still there: " enabled "Lowercasing alone doesn't remove the surrounding whitespace, " enabled " still won't equal "enabled". Both steps, trim and lowercase, are needed together.
Your task: given raw = " ENABLED ", normalize it and print the result.
Expected output:
enabledApproach
Every language has a trim method and a lowercase method; the difference is only in what they're called:
- Python:
.strip()removes leading/trailing whitespace,.lower()lowercases. - Java:
.trim()and.toLowerCase(), called the same way, chained together. - C++ doesn't have a built-in trim, so it's done manually with
<algorithm>, finding the first and last non-space character and keeping only what's between them. Lowercasing usesstd::transformwith::tolowerapplied to every character.
raw = " ENABLED "
normalized = raw.strip().lower()
print(normalized)