String Manipulation
Concatenate, index and slice text values.
Strings are sequences of characters. You can join strings, access individual characters and take slices from them.
Predict
Read the code first. Do not run it yet.
word = "PYTHON"
print(word[0])
print(word[1:4])
print(word[-1])- Predict all three outputs.
- Which index refers to the first character?
- In the slice
[1:4], is index 4 included?
Investigate
Use this code to explain exactly how the program works.
first = "Ada"
surname = "Lovelace"
full_name = first + " " + surname
print(full_name)
print(len(full_name))
print(full_name.upper())- Identify the concatenation operator.
- What does
len()count? - What changes and what stays the same when
upper()is used?
Concatenation
first = "Grace"
surname = "Hopper"
name = first + " " + surname
print(name)Grace Hopper
Indexing and slicing
text = "computer"
print(text[0])
print(text[2:5])
print(text[4:])c mpu uter
- String indexing begins at 0.
- A slice
[start:end]includesstartbut stops beforeend. - Leaving out the end takes the slice to the end of the string.
+ and slicing are essential. len(), upper() and lower() are also useful when processing text.Self-study guide
String manipulation means creating, joining and extracting text. OCR explicitly requires concatenation and slicing. The OCR Exam Reference Language also shows string length, substring, left/right extraction and upper/lower case operations.
The biggest translation difference is slicing. OCR .substring(x, i) uses a start index and a number of characters. Python slicing text[start:end] uses a start index and an end boundary that is excluded.
OCR Exam Reference Language ↔ Python
| OCR Exam Reference Language / term | Python | What to remember |
|---|---|---|
stringA + stringB | stringA + stringB | Concatenation uses + in both. |
subject.length | len(subject) | Length. |
subject.substring(3,5) | subject[3:8] | OCR uses start 3 and length 5; Python end boundary is 3+5 = 8. |
subject.left(4) | subject[:4] | First four characters. |
subject.right(3) | subject[-3:] | Last three characters. |
subject.upper | subject.upper() | Uppercase. |
subject.lower | subject.lower() | Lowercase. |
Worked example
Indexing and slicing both start from index 0.
subject = "ComputerScience"
first = subject[0]
chunk = subject[3:8]
left = subject[:4]
right = subject[-3:]
print(first)
print(chunk)
print(left)
print(right)C puter Comp nce
- Index 0 is the first character,
C. - The Python slice
[3:8]includes indexes 3, 4, 5, 6 and 7, producing five characters. - That corresponds to OCR
subject.substring(3,5). [:4]means from the start up to but not including index 4.[-3:]takes the final three characters.
Another example
Concatenation builds new text from smaller strings.
first_name = "Maya"
surname = "Stone"
username = first_name[0:2] + surname[-3:]
print(username.lower())maone
- The first slice gives
Ma. - The second slice gives
one. - Concatenation gives
Maone, then.lower()producesmaone.
Common mistakes
- Forgetting that Python slice end indexes are excluded.
- Treating OCR
.substring(x,i)as ifiwere an end index; in OCR ERL it is the number of characters. - Trying to concatenate a number to a string without converting the number first.
- Assuming
.upper()changes the original string automatically; it returns a new string value.
.substring(start, length) and Python [start:end] do not use the same second value.Exam-ready checklist
- Use zero-based indexing.
- Concatenate strings with
+. - Translate OCR substring/left/right operations into Python slicing.
- Use
len(),.upper()and.lower()correctly. - Work out the exact characters returned by a slice.
What you have learned
- Use
+to concatenate strings. - String indices start at 0.
- Use slicing to extract part of a string.
- The end position in a slice is excluded.