6.3.3 String manipulation
String Manipulation Operations
String manipulation is a core programming skill — programs frequently need to process text input, format output, validate data and extract sub-parts of strings. The four main operations required at GCSE are: length, position (indexing), substrings and case conversion.
Strings are zero-indexed: the first character is at index 0, the second at index 1, and so on.
Operation Reference
text = "Hello, World!"
# Length — number of characters including spaces and punctuation
print(len(text)) # 13
# Position (indexing) — zero-indexed
print(text[0]) # H (first character)
print(text[7]) # W
print(text[-1]) # ! (last character, negative indexing)
# Substring (slicing) — text[start:stop] stop is EXCLUSIVE
print(text[0:5]) # Hello (indices 0,1,2,3,4)
print(text[7:12]) # World
print(text[7:]) # World! (from index 7 to end)
# Case conversion
print(text.upper()) # HELLO, WORLD!
print(text.lower()) # hello, world!
# Find position of a substring (returns -1 if not found)
print(text.find("World")) # 7 (index where "World" starts)
# Check start/end
print(text.startswith("He")) # True
print(text.endswith("!")) # True
string text = "Hello, World!";
// Length
Console.WriteLine(text.Length); // 13
// Position (indexing) — zero-indexed
Console.WriteLine(text[0]); // H
Console.WriteLine(text[7]); // W
// Substring — Substring(startIndex, length)
Console.WriteLine(text.Substring(0, 5)); // Hello
Console.WriteLine(text.Substring(7, 5)); // World
// Case conversion
Console.WriteLine(text.ToUpper()); // HELLO, WORLD!
Console.WriteLine(text.ToLower()); // hello, world!
// Find
Console.WriteLine(text.IndexOf("World")); // 7
Dim text As String = "Hello, World!"
' Length
Console.WriteLine(text.Length) ' 13
' Position (indexing) — zero-indexed
Console.WriteLine(text(0)) ' H
Console.WriteLine(text(7)) ' W
' Substring — Substring(startIndex, length)
Console.WriteLine(text.Substring(0, 5)) ' Hello
Console.WriteLine(text.Substring(7, 5)) ' World
' Case conversion
Console.WriteLine(text.ToUpper()) ' HELLO, WORLD!
Console.WriteLine(text.ToLower()) ' hello, world!
' Find
Console.WriteLine(text.IndexOf("World")) ' 7
Practical Examples
# Generate a username: first 3 letters of first name + last name in lower case
first = input("First name: ")
last = input("Last name: ")
username = first[0:3].lower() + last.lower()
print("Username:", username)
# Example: "Alice" + "Smith" -> "alismith"
# Extract initials from a full name
full_name = input("Enter full name (first last): ")
parts = full_name.split() # split on spaces: ["Alice", "Smith"]
initials = parts[0][0].upper() + parts[1][0].upper()
print("Initials:", initials) # AS
Key Takeaways
- Strings are zero-indexed: first character at index 0.
len(s)— number of characters.s[i]— character at index i.- Python slicing:
s[start:stop]— stop index is exclusive. - C#/VB.NET:
Substring(startIndex, length)— second argument is length, not stop index. .upper()/.lower()return a new string — they do not modify the original.