![]() |
|
CHAPTER 9 — Strings & Text Manipulation - Printable Version +- The Lumin Archive (https://theluminarchive.co.uk) +-- Forum: The Lumin Archive — Core Forums (https://theluminarchive.co.uk/forumdisplay.php?fid=3) +--- Forum: Courses — Structured Learning (https://theluminarchive.co.uk/forumdisplay.php?fid=69) +---- Forum: Beginner’s Guide to Coding — Python From Zero to Skill (https://theluminarchive.co.uk/forumdisplay.php?fid=72) +---- Thread: CHAPTER 9 — Strings & Text Manipulation (/showthread.php?tid=227) |
CHAPTER 9 — Strings & Text Manipulation - Leejohnston - 11-15-2025 Chapter 9 — Strings & Text Manipulation Understanding how Python handles text — joining it, slicing it, editing it, and displaying it properly. A string is text inside quotation marks: Code: "Hello"Strings are everywhere: • names • messages • user input • menus • data files • website text --- 9.1 Creating Strings Use single or double quotes: Code: message = "Welcome!"Both work the same. --- 9.2 Multi-line Strings Use triple quotes: Code: story = """--- 9.3 Combining Strings (Concatenation) Code: first = "Lumin"Output: Code: Lumin Archive--- 9.4 Repeating Strings Code: print("Hi! " * 3)Output: Code: Hi! Hi! Hi!--- 9.5 Getting Length of a String Use len(): Code: name = "Johno"Output: Code: 5--- 9.6 Indexing (Getting a Single Character) Strings act like sequences: Code: name = "Python"Indexes start at 0. --- 9.7 Slicing (Getting Parts of a String) Code: word = "Harmonics"Format: Code: string[start:end]--- 9.8 Changing Case Code: text = "the lumin archive"--- 9.9 Useful String Methods Code: .strip() # remove spacesExamples: Code: msg = " hello world "--- 9.10 Formatting Strings (IMPORTANT) ## Method 1 — f-strings (recommended) Code: name = "Lee"## Method 2 — format() Code: print("Welcome, {}!".format("Mia"))--- 9.11 Escape Characters Use backslash for special characters: Code: print("He said \"Hello\" to me.")--- 9.12 Converting Types You CANNOT add text and numbers directly. ❌ Error: Code: age = 20✔ Fix: Code: print("Age: " + str(age))--- 9.13 Mini Project — Welcome Banner Ask the user: • name • favourite subject Output something like: Code: ====================--- 9.14 Challenge — Username Generator Ask for: • first name • last name • favourite number Create a username such as: mia_johnston_42 Rules: • everything lowercase • no spaces • use underscores --- 9.15 Chapter Summary • strings store text • indexing gets one character • slicing gets a portion • .upper(), .lower(), .strip(), .replace() are essential • f-strings make formatting easy • convert numbers to strings • escape characters help format output Next: Chapter 10 — Conditions & If Statements This is where programs start *thinking* — making decisions, choosing paths, and becoming interactive. --- Written and Compiled by Lee Johnston — Founder of The Lumin Archive |