Revise Computingrevisecomputing.co.uk
At a glanceFeaturesStudentsPricingHow it worksFree GCSE notesExam dates
At a glanceFeaturesStudentsPricingHow it worksFree GCSE notesExam dates

Knowledge organisers / Additional Programming Techniques

The use of basic string manipulation: slicing

All topicsPractise exam questions
Knowledge organiser

Additional Programming Techniques

2.2.3b

e.g. stringname.subString(startingPosition, numberOfCharacters)

What you need to know

String slicing extracts a portion (substring) of a string based on position indices. Strings are zero-indexed, meaning the first character is at position 0. You can also find the length of a string, convert it to uppercase or lowercase, and extract characters from the left or right.

Key points

  • Definition:String Manipulation: the process of performing operations on strings, such as concatenation, slicing, measuring length or changing case.
  • Strings are 0-indexed: the first character is at position 0, the second at position 1, etc.
  • .length gives the number of characters in a string. E.g. "Monday".length = 6.
  • .substring(start, count) extracts 'count' characters starting from 'start' position.
  • .left(n) returns the first n characters; .right(n) returns the last n characters.
  • .upper converts a string to UPPERCASE. E.g. "abcd1234".upper = "ABCD1234".
  • .lower converts a string to lowercase.
  • ASC() returns the ASCII code of a character; CHR() returns the character for a given code.
  • Concatenation joins strings together using +, e.g. pilotCode = a + b + str(c).
  • Common Mistake:Using a comma instead of + for concatenation. Commas in print() separate items, they DON'T join strings. Use + to concatenate.
  • Exam Example:message = "abcd1234" → message.left(4) = "abcd", message.right(4) = "1234", int(message.right(4)) * 2 = 2468.
  • Exam Tip:Username/ID generators are common. staffID = surname + str(year), then while staffID.length < 10: staffID = staffID + "x" pads it to length 10.
  • Exam Tip:str() converts a number to a string for concatenation: str(900) gives "900".

Code examples

String manipulation in Python
python
word = "ComputerScience"

print(len(word))       # 15 (length)
print(word[0:4])       # "Comp" (first 4 chars)
print(word[3:8])       # "puter" (index 3 to 7)
print(word[-3:])       # "nce" (last 3 chars)
print(word.upper())    # "COMPUTERSCIENCE"
print(word.lower())    # "computerscience"
print(ord("A"))        # 65 (ASCII code)
print(chr(97))         # 'a' (character from code)