.lower(), .upper(), .strip().replace().split() turns a string into a list.join() turns a list into a string
Names with stray spaces. Emails in MIXED case. A list jammed together with commas. Nobody hands you tidy text — you have to clean it yourself.
raw = " Aisha , ROHIT,sneha , Vikram "
# You want: ['Aisha', 'Rohit', 'Sneha', 'Vikram']
# String methods are how you get there.
value.method(). Strings come with dozens built in.
A string can never be changed in place. Every method hands you back a brand-new string and leaves the original untouched. So you must catch the result.
name = "aisha"
name.upper() # makes "AISHA"...
print(name) # ...but prints aisha 😟
# The new string was thrown away.
name = "aisha"
name = name.upper() # catch the result!
print(name) # prints AISHA ✅
text = text.something(), not just text.something().
"Hello".lower() # 'hello'
"Hello".upper() # 'HELLO'
"aisha khan".title() # 'Aisha Khan'
" spaces ".strip() # 'spaces' — ends trimmed
"xxhixx".strip("x") # 'hi' — strip any chars
.lower() makes comparisons fair: "YES" == "yes" is False, but "YES".lower() == "yes" is True.
.replace() — swap one piece for anotherGive it what to find and what to put in its place. It replaces every occurrence.
"2024-06-11".replace("-", "/") # '2024/06/11'
"I like cats".replace("cats", "dogs") # 'I like dogs'
"a b c d".replace(" ", "") # 'abcd' — delete spaces
"" and it simply vanishes.
.split() — string → listCuts a string wherever it finds the separator, and gives you back a list of the pieces.
"Aisha,Rohit,Sneha".split(",")
# → ['Aisha', 'Rohit', 'Sneha']
"the quick brown fox".split()
# → ['the', 'quick', 'brown', 'fox'] (no arg = split on spaces)
parts = "name:Aisha:age:19".split(":")
# → ['name', 'Aisha', 'age', '19']
.join() — list → stringThe exact opposite of split. The separator goes in front, and you hand it the list. Reads backwards the first few times — that's normal.
names = ["Aisha", "Rohit", "Sneha"]
", ".join(names) # 'Aisha, Rohit, Sneha'
"-".join(names) # 'Aisha-Rohit-Sneha'
"".join(names) # 'AishaRohitSneha'
# The glue ("separator") owns the .join — not the list.
Because each method returns a string, you can chain them — left to right, one transform after another.
raw = " Aisha , ROHIT,sneha , Vikram "
people = raw.split(",") # split on commas
clean = []
for p in people:
clean.append(p.strip().title()) # trim + tidy case, chained
print(clean)
# → ['Aisha', 'Rohit', 'Sneha', 'Vikram'] ✅
p.strip().title() runs strip first, then title on its result — a tiny pipeline on one line.
text.strip() alone does nothing..split() is for strings, .join() needs a list..join() backwards — the separator owns it: ", ".join(list)..lower() — "Yes" == "yes" is False.Given a string of comma-separated names with random spacing and case, output a clean, properly-capitalised list.
Take a messy sentence, strip it, fix the spacing, and rebuild it with join. ★ replace a banned word with "***".
students/<your-username>/D16_text_cleaner.py. Full brief in lab/module_2_practice/D16_text_cleaner/problem.md. PR before 12:00.
.strip(), .lower(), .replace().
.split() and back with .join().
slicing & indexing on Day 17.