GCSE Computer Science

Python string methods โ€” GCSE Computer Science

Length, indexing, slicing, and .upper()/.lower()/.strip()/.replace() are covered in the Python strings guide. This page covers the methods that go beyond the basics โ€” the ones that show up as their own exam questions.

.split() โ€” breaking a string into a list

sentence = "Python is fun"
words = sentence.split(" ")
print(words)   # ['Python', 'is', 'fun']

.split() breaks a string into a list, cutting at every occurrence of the character you give it. Leave the brackets empty and it splits on any whitespace by default. It's tested on every exam board โ€” usually as part of processing a line of input into separate values.

.find() โ€” locating a substring

word = "Computer Science"
print(word.find("Science"))   # 9
print(word.find("Maths"))     # -1

.find() returns the index where a substring starts. If the substring isn't there at all, it doesn't raise an error โ€” it returns -1.

.count() โ€” counting occurrences

text = "banana"
print(text.count("a"))   # 3

.count() returns how many times a substring appears. It's case-sensitive, and it counts every occurrence, not just the first.

ord() and chr() โ€” characters and their codes

Every character has a numeric code behind it โ€” its ASCII or Unicode value. ord() converts a character to its code; chr() does the reverse.

print(ord("A"))   # 65
print(chr(65))    # A

ord() and chr() are exact opposites of each other. This pair is specified on OCR and AQA in particular โ€” check what your board expects, but it's worth knowing both regardless.

Common exam mistake

Expecting .find() to raise an error when the substring isn't there. It returns -1 silently. Students who expect an exception don't check the return value, and their code carries on treating -1 as if it were a valid index.

What examiners actually test

Questions typically give you a string and ask you to predict the output of applying one of these methods, or ask you to write code that uses one to solve a small problem โ€” finding a substring's position, splitting a sentence into words, converting between a letter and its character code. Know what each method returns, not just what it does โ€” .find() returning -1 instead of raising an error is the detail most likely to catch you out.

Python Coach covers hundreds of challenges taking students from their first print statement to advanced Python โ€” with progress tracking for every student.

Start your school's free trial →