upper(), lower(), strip(), replace(), split() and join() — plus f-strings, the cleanest way to mix text and numbers in one readable line.
01
Learning objectives
Call the common string methods and state that each returns a NEW string.
Clean user input with strip() and lower()/upper() before comparing.
Break a sentence into words with split() and rebuild text with join().
Substitute values into text with f-strings, including formatting numbers as {value:.2f}.
Choose between + concatenation and f-strings and justify the choice.
02
Lesson notes
A method is a function attached to a value and called with a dot: name.upper(), sentence.split(). Strings ship with a whole toolbox of them, and five do most of the work. upper() and lower() change case. strip() removes surrounding whitespace — invisible spaces users accidentally type around input are the classic bug it prevents. replace(old, new) substitutes every occurrence. split() cuts a string into a list of pieces (default: cut at every run of whitespace), and join() glues a list of strings back together with a chosen glue: " ".join(words).
The law that governs all of them is immutability from last lesson: every string method returns a new string and leaves the original untouched. name = "ayesha" followed by name.upper() alone prints nothing and changes nothing — the new string "AYESHA" is created and dropped. The working pattern is assignment: name = name.upper(). This one sentence — 'methods return new strings' — is the most-marked misconception in the strings chapter, and it follows directly from the TypeError you saw with word[0] = "J".
f-strings are Python's modern answer to building messages: prefix the quote with f and drop variables — or any expression — inside braces. f"Name: {name}", f"Total: {price * qty}", f"Next year: {age + 1}". Inside the braces you may compute, call functions and index — the expression runs and its result is placed into the text. Compared with "Total: " + str(total), the f-string version needs no casting gymnastics, keeps the sentence in one readable piece, and refuses to raise the str+int TypeError because substitution handles every type.
F-strings also format numbers on request with a format specifier after a colon: {percentage:.2f} means 'as a fixed-point number with exactly 2 decimal places'. f"{81.45454:.2f}%" prints 81.45%; f"{1234567.891:,.2f}" adds thousands separators: 1,234,567.89. Rounding for display is not the same as rounding the stored value — percentage keeps its full precision and only the output is tidied, which is exactly what a good report wants. Padding and alignment ({name:>10}) exist too, but :.2f is the workhorse for this course.
Put the toolbox together and every input-handling chore becomes a one-liner: normalise a search term with query.strip().lower(), count the words in a paragraph with len(paragraph.split()), initials with full[0] + "." after splitting, and clean reports with f-strings. These five methods plus f-strings cover approximately everything the board asks under 'string handling' — and the Labs' io-strings tier drills each one until it is automatic.
03
Worked code examples
Cleaning input before comparing
The fed input carries leading/trailing spaces and mixed case. strip() removes the edges, lower() normalises the case — and the comparison succeeds. Cleaning input is the first step of every robust program.
split() with no argument cuts at whitespace runs, returning a list of 4 words. join() glues them back with any separator — here a hyphen, and reversed() feeds the words back in opposite order.
sentence = "ICS students learn Python"
words = sentence.split()
print(len(words), words)
print("-".join(words))
print(" ".join(reversed(words)))
Output
4 ['ICS', 'students', 'learn', 'Python']
ICS-students-learn-Python
Python learn students ICS
f-strings with expressions and :.2f
Anything inside the braces is evaluated — variables, arithmetic, even new expressions. The :.2f specifier trims the display to 2 decimal places while the stored value keeps full precision.
name = "Ayesha"
obtained, total = 448, 550
percentage = obtained / total * 100
print(f"{name} scored {obtained}/{total}")
print(f"Percentage: {percentage:.2f}%")
print(f"Next year target: {percentage + 5:.1f}%")
Output
Ayesha scored 448/550
Percentage: 81.45%
Next year target: 86.5%
04
Key terminology
method
A function attached to a value, called with a dot: name.upper(), sentence.split().
strip()
Returns the string without leading/trailing whitespace — the standard input-cleaner.
split()
Cuts a string into a list of pieces; with no argument it splits at every whitespace run.
join()
Glues a list of strings together with the given separator: "-".join(items).
f-string
A string prefixed with f that evaluates {expressions} inline; {x:.2f} formats a number to 2 decimal places.
05
Common misconceptions
Common mistake
name.upper() changes the variable name in place.
Correct understanding
Every string method returns a NEW string — capture it: name = name.upper(). The original is never modified.
Common mistake
split() always splits on commas.
Correct understanding
With no argument, split() cuts at any run of whitespace. To split on commas you must ask: s.split(",").
Common mistake
f"{x:.2f}" changes x to 2 decimal places.
Correct understanding
It only formats the DISPLAY of x inside that string; x keeps its full precision. Use round(x, 2) to actually round the stored value.
06
Check your understanding
1. s = " hello ". What is s.strip()?
2. After city = "lahore" and city.upper(), what does print(city) show?
3. Which print shows the value of average with exactly 2 decimal places?