>> Module 02 / Day 16 / Lecture

String Methods

Module 2 begins — Python in practice
Module 1 taught you to think in Python. Module 2 is about the messy real world — and the real world is full of text.
Lecture
75 min · 09:00–10:15
Lab
60 min · Clean up messy text
Module
M2 · Python in Practice
DAY 16 / 40
>> Today's roadmap

Six tools for taming text

  1. The golden rule — strings never change, methods return new ones
  2. Case & whitespace — .lower(), .upper(), .strip()
  3. Find & replace — .replace()
  4. Breaking apart — .split() turns a string into a list
  5. Sticking together — .join() turns a list into a string
  6. Lab — clean up a block of messy text
>> The hook

Real data is always messy.

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.

the_mess.py
raw = "  Aisha , ROHIT,sneha  ,  Vikram "
# You want: ['Aisha', 'Rohit', 'Sneha', 'Vikram']
# String methods are how you get there.
>> 01 / 05 · The golden rule

Methods return a new string

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.

does_nothing.py
name = "aisha"
name.upper()        # makes "AISHA"...
print(name)         # ...but prints aisha 😟
# The new string was thrown away.
does_it_right.py
name = "aisha"
name = name.upper()  # catch the result!
print(name)         # prints AISHA ✅
>> 02 / 05 · Case & whitespace

Tidy up case and stray spaces

tidy.py
"Hello".lower()       # 'hello'
"Hello".upper()       # 'HELLO'
"aisha khan".title() # 'Aisha Khan'

"   spaces   ".strip()  # 'spaces' — ends trimmed
"xxhixx".strip("x")    # 'hi' — strip any chars
Why it matters.
.lower() makes comparisons fair: "YES" == "yes" is False, but "YES".lower() == "yes" is True.
>> 03 / 05 · Find & replace

.replace() — swap one piece for another

Give it what to find and what to put in its place. It replaces every occurrence.

replace.py
"2024-06-11".replace("-", "/")   # '2024/06/11'
"I like cats".replace("cats", "dogs")  # 'I like dogs'
"a b c d".replace(" ", "")      # 'abcd' — delete spaces
>> 04 / 05 · Breaking apart

.split() — string → list

Cuts a string wherever it finds the separator, and gives you back a list of the pieces.

split.py
"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']
>> 05 / 05 · Sticking together

.join() — list → string

The exact opposite of split. The separator goes in front, and you hand it the list. Reads backwards the first few times — that's normal.

join.py
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.
>> Putting it together

A cleaning pipeline

Because each method returns a string, you can chain them — left to right, one transform after another.

pipeline.py
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'] ✅
>> Common failures

Four mistakes you'll make today

  1. Forgetting to catch the result — text.strip() alone does nothing.
  2. Calling a string method on a list (or vice-versa) — .split() is for strings, .join() needs a list.
  3. Getting .join() backwards — the separator owns it: ", ".join(list).
  4. Comparing text without .lower() — "Yes" == "yes" is False.
>> 11:00 – 12:00 · Lab

Today's lab — Clean Up Messy Text

PROBLEM 1
Name Cleaner

Given a string of comma-separated names with random spacing and case, output a clean, properly-capitalised list.

PROBLEM 2
Sentence Fixer

Take a messy sentence, strip it, fix the spacing, and rebuild it with join. ★ replace a banned word with "***".

>> End of Day 16

What you can now do

  • ✓ Clean text with .strip(), .lower(), .replace().
  • ✓ Turn a string into a list with .split() and back with .join().
  • ✓ Remember that strings are immutable — always catch the result.
01 / 12
←/→ navigate   F fullscreen   Ctrl/⌘+P save as PDF