§ 2.3section 5 of 18·~4 min
Working with strings
Most data cleaning is string work: trimming spaces, fixing case, splitting fields apart and building new text.
name = " Priya Sharma "
clean = name.strip().lower()
print(clean)
print(clean.split(" "))
print("2026-10-04".replace("-", "/"))
city = "Pune"
print(f"{clean} lives in {city}")
OUTPUT
priya sharma
['priya', 'sharma']
2026/10/04
priya sharma lives in Pune| Method | What it does |
|---|---|
.strip() | Removes spaces from both ends |
.lower() / .upper() | Changes the case |
.split(sep) | Breaks text into a list at sep |
.replace(a, b) | Swaps every a for b |
f"...{x}..." | Puts the value of x inside the text |
watch out "Pune" == "pune " is False. Untrimmed, inconsistently cased strings cause a lot of failed joins and duplicate rows. Normalise them early.