OCR Computing A-Level Revision

String Manipulation (2.4.g)

This section is programming language specific - I am using Python 2.7.

All programming languages have simple functions for manipulating strings. Below is the syntax for Python 2 (read the docs):

Function Description Example
len(string) Returns the length of the input string, which can be stored in a variable. len("This is a simple sentence") 25
len(test) 44
str.replace(old, new, [count]) Replaces a substring with another substring (a given number of times). test.replace("quick", "slow")  "the slow brown fox jumped over the lazy dog"
str.count(substring, [start], [end]) Counts the number of times the substring appears (within a given range). test.count("the") 2
str.find(substring, [start], [end]) Returns the index of the (first occurrence of) the substring. test.find("brown") 10
str[index] Returns the character in position index of str. test[0] t
test[-2] o
str.split(separator, [max splits]) Splits the string at every occourance of separator into a list (optionally up to a maximum of max splits) test.split("fox") ["the quick brown ", " jumped over the lazy dog"]
test.split(" ", 5) ["the", "quick", "brown", "fox", "jumped","over the lazy dog"
ord(char) Returns the unicode ID of char. n.b. if the character is also in ASCII, then the ASCII ID will be the same as the unicode ID. ord("z") 122
chr(int) Returns the character which has a unicode ID of int. chr(122) "z"

n.b. In all the examples above, the variable "test" contains the string "the quick brown fox jumped over the lazy dog".

Character Codes (2.4.h)

In ASCII, each character corresponds to a binary character code. For example, "a" corresponds to 97, and 101 corresponds to "d". Therefore, "a" < "d", as "a" is 97, and "d" is 101. However, "A" != "a", as "A" is 65 (whereas "a" is 97).