11-15-2025, 10:29 PM
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:
Strings are everywhere:
• names
• messages
• user input
• menus
• data files
• website text
---
9.1 Creating Strings
Use single or double quotes:
Both work the same.
---
9.2 Multi-line Strings
Use triple quotes:
---
9.3 Combining Strings (Concatenation)
Output:
---
9.4 Repeating Strings
Output:
---
9.5 Getting Length of a String
Use len():
Output:
---
9.6 Indexing (Getting a Single Character)
Strings act like sequences:
Indexes start at 0.
---
9.7 Slicing (Getting Parts of a String)
Format:
---
9.8 Changing Case
---
9.9 Useful String Methods
Examples:
---
9.10 Formatting Strings (IMPORTANT)
## Method 1 — f-strings (recommended)
## Method 2 — format()
---
9.11 Escape Characters
Use backslash for special characters:
---
9.12 Converting Types
You CANNOT add text and numbers directly.
❌ Error:
✔ Fix:
---
9.13 Mini Project — Welcome Banner
Ask the user:
• name
• favourite subject
Output something like:
---
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
Understanding how Python handles text — joining it, slicing it, editing it, and displaying it properly.
A string is text inside quotation marks:
Code:
"Hello"
'Python'
"123"
"Lee Johnston"Strings are everywhere:
• names
• messages
• user input
• menus
• data files
• website text
---
9.1 Creating Strings
Use single or double quotes:
Code:
message = "Welcome!"
note = 'Python is fun.'Both work the same.
---
9.2 Multi-line Strings
Use triple quotes:
Code:
story = """
This is line one.
This is line two.
"""---
9.3 Combining Strings (Concatenation)
Code:
first = "Lumin"
second = "Archive"
title = first + " " + secondOutput:
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"
print(len(name))Output:
Code:
5---
9.6 Indexing (Getting a Single Character)
Strings act like sequences:
Code:
name = "Python"
print(name[0]) # P
print(name[3]) # hIndexes start at 0.
---
9.7 Slicing (Getting Parts of a String)
Code:
word = "Harmonics"
print(word[0:4]) # Harm
print(word[4:]) # onicsFormat:
Code:
string[start:end]---
9.8 Changing Case
Code:
text = "the lumin archive"
print(text.upper()) # THE LUMIN ARCHIVE
print(text.title()) # The Lumin Archive
print(text.lower()) # the lumin archive---
9.9 Useful String Methods
Code:
.strip() # remove spaces
.replace() # replace text
.startswith()
.endswith()Examples:
Code:
msg = " hello world "
print(msg.strip()) # "hello world"
print(msg.replace("world", "Johno"))---
9.10 Formatting Strings (IMPORTANT)
## Method 1 — f-strings (recommended)
Code:
name = "Lee"
score = 42
print(f"{name} scored {score} points!")## Method 2 — format()
Code:
print("Welcome, {}!".format("Mia"))---
9.11 Escape Characters
Use backslash for special characters:
Code:
print("He said \"Hello\" to me.")
print("Line one\nLine two")---
9.12 Converting Types
You CANNOT add text and numbers directly.
❌ Error:
Code:
age = 20
print("Age: " + age)✔ Fix:
Code:
print("Age: " + str(age))---
9.13 Mini Project — Welcome Banner
Ask the user:
• name
• favourite subject
Output something like:
Code:
====================
Welcome, Mia!
Your favourite subject is: Science
====================---
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
