String Manipulation
Learn to work with text data by treating strings as arrays of characters.
What you'll learn:
- Why strings are called "strings" (they're arrays of characters!)
- Accessing individual characters using index notation
- String slicing to extract substrings
- Concatenation - joining strings together
- Useful string methods: len(), upper(), lower(), find()
Why Are They Called "Strings"?
In programming, we call text data a "string" rather than "text" because it's actually a sequence of characters strung together - like beads on a string!
Strings ARE Arrays!
Because strings are arrays of characters, you can use the same techniques you learned with arrays:
Arrays
numbers = [10, 20, 30]numbers[0] → 10len(numbers) → 3
Strings
word = "CAT"word[0] → "C"len(word) → 3
String Operations
Accessing Characters
Use square brackets with an index to get a single character.
word = "HELLO"
print(word[0]) # H (first character)
print(word[4]) # O (fifth character)
print(word[-1]) # O (last character)
print(word[-2]) # L (second to last)
String Slicing
Extract a substring using [start:end]. End is NOT included!
text = "PYTHON"
print(text[0:3]) # PYT (positions 0,1,2)
print(text[:3]) # PYT (from start to 2)
print(text[3:]) # HON (from 3 to end)
print(text[-3:]) # HON (last 3 chars)
Concatenation
Join strings together using the + operator.
first = "Hello"
second = "World"
result = first + " " + second
print(result) # Hello World
name = "Alice"
greeting = "Hi, " + name + "!"
print(greeting) # Hi, Alice!
String Length
Use len() to find how many characters are in a string.
word = "Python"
length = len(word)
print(length) # 6
# Useful for loops!
for i in range(len(word)):
print(word[i])
Useful String Methods
| Method | What it does | Example | Result |
|---|---|---|---|
len(s) |
Returns the length (number of characters) | len("Hello") |
5 |
s.upper() |
Converts to UPPERCASE | "hello".upper() |
"HELLO" |
s.lower() |
Converts to lowercase | "HELLO".lower() |
"hello" |
s.find(x) |
Finds position of substring (-1 if not found) | "hello".find("l") |
2 |
s.replace(a, b) |
Replaces all occurrences of a with b | "hello".replace("l", "x") |
"hexxo" |
s.strip() |
Removes whitespace from start and end | " hi ".strip() |
"hi" |
x in s |
Checks if substring exists (True/False) | "el" in "hello" |
True |
String Slicing Guide
text[0]
→
"C"
First character
text[-1]
→
"G"
Last character
text[:3]
→
"COM"
First 3 characters
text[-3:]
→
"ING"
Last 3 characters
text[2:6]
→
"MPUT"
From index 2 to 5
Learning Path
Follow these activities in order.
1. Predict: String Output
Predict what string operations will produce.
2. Study: Worked Example
See how to manipulate strings step by step.
3. Investigate: Trace String Operations
Track how strings change through operations.
4. Modify: Arrange the Code
Put code blocks in order for string manipulation.
5. Test: Knowledge Quiz
Check your understanding of string manipulation.
Key Vocabulary
string
A sequence of characters - like an array of letters. Called "string" because characters are strung together.
index
The position of a character in a string. Starts at 0, not 1!
slicing
Extracting a portion of a string using [start:end] notation.
concatenation
Joining strings together using the + operator.
substring
A smaller string extracted from a larger string.
immutable
Cannot be changed. Strings are immutable - you create new strings instead of modifying existing ones.
Quick Reference
# Strings are arrays of characters
text = "Python"
# Access single characters
first = text[0] # "P"
last = text[-1] # "n"
# Slicing (extracting substrings)
start = text[:3] # "Pyt" (first 3)
end = text[-3:] # "hon" (last 3)
middle = text[1:4] # "yth" (positions 1,2,3)
# Concatenation
greeting = "Hello" + " " + "World" # "Hello World"
# Length
length = len(text) # 6
# Case conversion
upper = text.upper() # "PYTHON"
lower = text.lower() # "python"
# Finding substrings
pos = text.find("th") # 2 (position where "th" starts)
exists = "th" in text # True
# Loop through characters (just like an array!)
for char in text:
print(char)