11-15-2025, 10:25 PM
Chapter 6 — Strings
Understanding and controlling text in Python
A string is simply text inside quotation marks.
Strings are used for:
• names
• messages
• sentences
• user input
• filenames
• any text your program displays
Python gives you powerful tools to create and manipulate strings.
---
6.1 Creating Strings
Strings always go inside quotes:
You can use:
• double quotes " "
• or single quotes ' '
Both are fine:
---
6.2 Printing Strings
Simple:
Or using variables:
---
6.3 Combining Strings (Concatenation)
Use + to join strings:
Output:
Remember:
You can only add strings to strings.
This fails:
Fix using str():
---
6.4 Repeating Strings
Python lets you multiply a string:
Output:
Useful for patterns, dividers, or simple banners.
---
6.5 Escape Characters (Special Symbols)
Sometimes you need quotation marks *inside* your string.
Python uses backslashes to escape them:
Common escape codes:
• \n — new line
• \t — tab
• \" — quote
• \\ — backslash
Example:
---
6.6 String Length
Use len() to measure the number of characters:
Output:
Counting includes:
• spaces
• letters
• numbers
• punctuation
---
6.7 Indexing — Getting Individual Characters
Each character has a position:
Important:
• Python starts counting at 0
• Last character = index -1
Example:
---
6.8 Slicing — Getting Parts of a String
Format:
Example:
Start is included.
End is NOT included.
Shortcut examples:
---
6.9 Changing Case
Python has built-in string functions:
---
6.10 Removing Spaces
Stripping spaces from ends:
Other options:
• .lstrip() — left only
• .rstrip() — right only
---
6.11 Finding Substrings
Check if part of a string exists:
---
6.12 Replacing Parts of a String
---
6.13 Exercise — Sentence Tools
Create a file called string_tools.py.
Ask the user for a sentence.
Then print:
1. The length of the sentence
2. The first character
3. The last character
4. The sentence in uppercase
5. The sentence reversed
(Hint: reversed string =
)
---
6.14 Small Challenge
Ask the user for:
• their first name
• their last name
Then output:
(Hint: first character of each name)
---
6.15 Chapter Summary
• Strings store text
• Use + to combine strings
• Use * to repeat strings
• Escape characters allow quotes, new lines, etc.
• len() counts characters
• Indexing gets single letters
• Slicing gets parts of a string
• .upper(), .lower(), .title() change case
• .strip() removes spaces
• .replace() replaces text
Next:
Chapter 7 — Numbers & Maths in Python
Now your programs begin to calculate real values.
---
Written and Compiled by Lee Johnston — Founder of The Lumin Archive
Understanding and controlling text in Python
A string is simply text inside quotation marks.
Strings are used for:
• names
• messages
• sentences
• user input
• filenames
• any text your program displays
Python gives you powerful tools to create and manipulate strings.
---
6.1 Creating Strings
Strings always go inside quotes:
Code:
name = "Johno"
colour = "blue"
message = "Welcome to Python!"You can use:
• double quotes " "
• or single quotes ' '
Both are fine:
Code:
word1 = "hello"
word2 = 'hello'---
6.2 Printing Strings
Simple:
Code:
print("Hello there!")Or using variables:
Code:
name = "Mia"
print(name)---
6.3 Combining Strings (Concatenation)
Use + to join strings:
Code:
first = "Hello"
second = "world"
print(first + " " + second)Output:
Code:
Hello worldRemember:
You can only add strings to strings.
This fails:
Code:
age = 16
print("Age: " + age) # ERRORFix using str():
Code:
age = 16
print("Age: " + str(age)) # Works---
6.4 Repeating Strings
Python lets you multiply a string:
Code:
print("ha" * 3)Output:
Code:
hahahaUseful for patterns, dividers, or simple banners.
---
6.5 Escape Characters (Special Symbols)
Sometimes you need quotation marks *inside* your string.
Python uses backslashes to escape them:
Code:
print("He said \"Hello!\"")Common escape codes:
• \n — new line
• \t — tab
• \" — quote
• \\ — backslash
Example:
Code:
print("Line 1\nLine 2")---
6.6 String Length
Use len() to measure the number of characters:
Code:
word = "Python"
print(len(word))Output:
Code:
6Counting includes:
• spaces
• letters
• numbers
• punctuation
---
6.7 Indexing — Getting Individual Characters
Each character has a position:
Code:
text = "hello"
print(text[0]) # h
print(text[1]) # eImportant:
• Python starts counting at 0
• Last character = index -1
Example:
Code:
print(text[-1]) # o---
6.8 Slicing — Getting Parts of a String
Format:
Code:
string[start:end]Example:
Code:
word = "Python"
print(word[0:3]) # Pyt
print(word[2:6]) # thonStart is included.
End is NOT included.
Shortcut examples:
Code:
print(word[:3]) # from start to position 3
print(word[3:]) # from position 3 to end---
6.9 Changing Case
Python has built-in string functions:
Code:
message = "hello world"
print(message.upper()) # HELLO WORLD
print(message.lower()) # hello world
print(message.title()) # Hello World---
6.10 Removing Spaces
Stripping spaces from ends:
Code:
text = " hello "
print(text.strip()) # "hello"Other options:
• .lstrip() — left only
• .rstrip() — right only
---
6.11 Finding Substrings
Check if part of a string exists:
Code:
sentence = "Python is powerful"
print("power" in sentence) # True
print("weak" in sentence) # False---
6.12 Replacing Parts of a String
Code:
text = "I like apples"
new_text = text.replace("apples", "oranges")
print(new_text)---
6.13 Exercise — Sentence Tools
Create a file called string_tools.py.
Ask the user for a sentence.
Then print:
1. The length of the sentence
2. The first character
3. The last character
4. The sentence in uppercase
5. The sentence reversed
(Hint: reversed string =
Code:
text[::-1]---
6.14 Small Challenge
Ask the user for:
• their first name
• their last name
Then output:
Code:
"Initials: F.L."(Hint: first character of each name)
---
6.15 Chapter Summary
• Strings store text
• Use + to combine strings
• Use * to repeat strings
• Escape characters allow quotes, new lines, etc.
• len() counts characters
• Indexing gets single letters
• Slicing gets parts of a string
• .upper(), .lower(), .title() change case
• .strip() removes spaces
• .replace() replaces text
Next:
Chapter 7 — Numbers & Maths in Python
Now your programs begin to calculate real values.
---
Written and Compiled by Lee Johnston — Founder of The Lumin Archive
