Strings — shaping text

A school notice is text you clean: capitals for a banner, a name dropped into a sentence, a line split into words. Strings are not just print decorations — they are data. Attendance files, WhatsApp exports and log lines are strings until you parse them.

Class notice message

Strings are text you can shape.

You will be able to

  • Join strings and use f-strings
  • Use upper, lower and replace
  • Treat text as something you can transform

f-strings fill blanks

f"Hello {name}" inserts the variable into the sentence. It stays readable when a notice has three blanks. Comma-print is fine for two values; f-strings win for prose.

The f is not decoration. Without it, {name} would print as the characters {name}. With it, Python evaluates the braces.

Methods hang on the string

"stem".upper() returns "STEM". The original string in a variable is not always changed — in real Python strings are immutable, so you store the result: banner = msg.upper().

replace(old, new) is search-and-replace for a notice. split() turns a sentence into a list of words — the start of counting word frequency, which pandas will later do in bulk.

Words that matter

f-string
A string prefixed with f that can include {expressions}.
Immutable
In real Python, string methods return new strings; they do not edit in place.

Common mistakes

Avoid: "Hello {name}" without the f prefix.

Do this: f"Hello {name}".

Example program — House announcement

Edit the house name; the sentence updates.

Python sandboxlesson://workspace
console

Edit the example, press Run, then Build if you want a compile check.

build

Press Build to compile.

Your turn — ID card line

name = "kabir". Print the name in uppercase so KABIR appears.

Python sandboxlesson://workspace
console

Edit the example, press Run, then Build if you want a compile check.

build

Press Build to compile.

Self-assessment

Check your understanding before you mark the lesson complete.

1. "stem".upper() becomes…
2. f-strings need…

Progress is stored in a browser cookie on this device.