The Lumin Archive
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"
'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 + " " + second

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"
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])  # h

Indexes start at 0.

---

9.7 Slicing (Getting Parts of a String)

Code:
word = "Harmonics"
print(word[0:4])  # Harm
print(word[4:])    # onics

Format:
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