1.2.12Introduction to Programming (Python)

String methods — upper, lower, strip, split, join, replace, find, format

1,877 words9 min readdifficulty · medium

WHAT each method does (and HOW it works)


Deriving the behaviour from first principles

You don't need to memorise these — you can reconstruct them. A string is a sequence of characters indexed 0,1,2,.... Every method is just a loop over those characters:

Figure — String methods — upper, lower, strip, split, join, replace, find, format

Worked examples


Common mistakes (Steel-manned)


Active recall

Recall Try before peeking
  1. Why must you reassign after s.strip()?
  2. What does find return when the substring is missing?
  3. What separates split() from split(' ')?
  4. Write join that puts - between ['a','b','c'].
Recall Explain to a 12-year-old (Feynman)

Imagine a string is a row of LEGO letters that's been super-glued — you can never rearrange the original row. So every "tool" actually builds a fresh copy for you. upper makes a shouty copy, strip makes a copy with the empty edges snipped off, split snaps the row into a basket of smaller rows at every comma, and join glues a basket back into one row putting a chosen sticker between each piece. Because they all hand you a new row, you must keep it (name = ...) or it floats away.


Flashcards

Are Python strings mutable or immutable?
Immutable — every method returns a NEW string.
What does s.upper() do to digits/symbols?
Nothing; only letters are cased.
What does s.strip() remove?
Leading and trailing whitespace (or any chars in strip(chars)).
Difference: "a b".split() vs "a b".split(' ')?
['a','b'] (collapses) vs ['a','','b'] (keeps empty).
What does sep.join(list) do?
Glues list items into one string with sep between them (inverse of split).
How many separators does join insert for n items?
n − 1 (between items only).
What does s.replace("a","b") replace?
EVERY occurrence (add a count arg to limit).
What does s.find("x") return if "x" is absent?
-1 (a sentinel, not an error).
Why is if s.find("a"): buggy?
A match at index 0 is falsy; use != -1 or the in operator.
What does "{:.1%}".format(0.5) give?
'50.0%' (×100, 1 decimal, percent sign).
Last valid start index when finding length-m sub in length-n string?
n − m.
Why reassign after name.strip()?
The original is unchanged; the cleaned copy is the return value.

Connections

  • Strings — indexing and slicing (find relies on s[i:i+m])
  • Lists (split produces a list, join consumes one)
  • Immutability in Python
  • f-strings (modern alternative to .format)
  • Boolean truthiness (why 0/-1 traps with find)
  • User input and validation

Concept Map

needs cleaning

so every method

returns new string

forces

inverse of

slides window i:i+m

produces

Messy raw text

Strings are immutable

String methods toolbox

Must reassign result

upper / lower

strip removes whitespace

split into list

join glues list

replace old with new

find returns index or -1

format fills placeholders

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, Python me strings immutable hote hain — matlab ek baar bana diya toh use badal nahi sakte. Isiliye jab tum name.strip() likhte ho, woh original string ko clean nahi karta, balki ek nayi cleaned copy return karta hai. Beginners ki sabse badi galti yahi hai: name.strip() likh dete hain par name = name.strip() nahi likhte, aur phir confuse hote hain ki kuch change kyu nahi hua. Yaad rakho — return value capture karna zaroori hai.

Ab har method ka kaam simple hai. upper/lower casing fix karte hain (useful jab user "ALICE" likhe aur tum "alice" se compare karna chahte ho). strip edges ke extra spaces aur \n hata deta hai — input cleaning ke liye must. split ek string ko comma ya space par tod kar list bana deta hai, aur join uska ulta hai: list ko wapas ek string me jod deta hai. Mnemonic yaad rakho: SLuSH-J (Strip → Lower → Split → Join).

Do trap zaroor samjho. Pehla: find True/False nahi, balki index return karta hai, aur agar substring nahi mila toh -1 deta hai. Toh if s.find("a"): likhna galat hai kyunki index 0 falsy hota hai — hamesha != -1 check karo ya in operator use karo. Doosra: split() (bina argument) spaces ke runs ko collapse kar deta hai, par split(' ') empty pieces rakh leta hai. Human text ke liye bare split() best hai.

Practical life me ye methods data cleaning ka 80% kaam karte hain — CSV parsing, user input validate karna, report banana. Inhe ratna mat, balki my_find aur my_join jaise chhote loops khud likh ke samjho; tab tum kabhi nahi bhuloge.

Go deeper — visual, from zero

Test yourself — Introduction to Programming (Python)

Connections