07-string-manipulation.py
python-programming / programming-foundations  ·  Lesson 07
Lesson 07 — Programming foundations

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.

string-manipulation-predict.py
word = "PYTHON"
print(word[0])
print(word[1:4])
print(word[-1])
Predict before running
  • 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.

string-manipulation-investigate.py
first = "Ada"
surname = "Lovelace"
full_name = first + " " + surname
print(full_name)
print(len(full_name))
print(full_name.upper())
Investigate the code
  • Identify the concatenation operator.
  • What does len() count?
  • What changes and what stays the same when upper() is used?

Concatenation

join.py
first = "Grace"
surname = "Hopper"
name = first + " " + surname
print(name)
Output
Grace Hopper

Indexing and slicing

slice.py
text = "computer"
print(text[0])
print(text[2:5])
print(text[4:])
Output
c
mpu
uter
How it works
  • String indexing begins at 0.
  • A slice [start:end] includes start but stops before end.
  • Leaving out the end takes the slice to the end of the string.
Useful string operationsConcatenation with + 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 focusBe able to concatenate and slice strings. Remember that OCR strings are zero-indexed, and translate OCR substring operations carefully into Python slice boundaries.

OCR Exam Reference Language ↔ Python

OCR Exam Reference Language / termPythonWhat to remember
stringA + stringBstringA + stringBConcatenation uses + in both.
subject.lengthlen(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.uppersubject.upper()Uppercase.
subject.lowersubject.lower()Lowercase.
Read both formsIn an examination you may see OCR Exam Reference Language even when you have learned the idea in Python. Translate the structure, not just the symbols.

Worked example

Indexing and slicing both start from index 0.

lesson-07-worked.py
subject = "ComputerScience"

first = subject[0]
chunk = subject[3:8]
left = subject[:4]
right = subject[-3:]

print(first)
print(chunk)
print(left)
print(right)
Output
C
puter
Comp
nce
Work through it line by line
  • 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.

lesson-07-example-2.py
first_name = "Maya"
surname = "Stone"
username = first_name[0:2] + surname[-3:]
print(username.lower())
Output
maone
Why this example matters
  • The first slice gives Ma.
  • The second slice gives one.
  • Concatenation gives Maone, then .lower() produces maone.

Common mistakes

Avoid these errors
  • Forgetting that Python slice end indexes are excluded.
  • Treating OCR .substring(x,i) as if i were 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.
Exam-safe warningOCR .substring(start, length) and Python [start:end] do not use the same second value.

Exam-ready checklist

You should now be able to
  • 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.