Length, indexing, substring, case conversion, ASCII — in OCR ERL and Python
length / len(), and access individual characters using 0-based indexingord() / chr() in Python) to work with ASCII character codes+ operator. The strings are joined end-to-end in the order given. You can concatenate string variables, string literals, and the results of string functions."Age: " + 16 causes an error — you cannot use + to join a string and an integer. Cast the integer first: "Age: " + str(16)myString.length (OCR ERL) or len(myString) returns the total number of characters in the string — including spaces and symbols.myString.substring(start, length) returns a portion of a string. start is the 0-based index of the first character to extract. length is how many characters to return.substring(start, length) — second argument is how many characters.[start:end] — second number is the stop index (exclusive), not the length.myString.upper() returns a new string with all letters converted to UPPERCASE. myString.lower() returns a new string with all letters converted to lowercase. The original string is not changed — a new string is returned.| Function | Converts | Example |
|---|---|---|
| int() | str/float → integer | int("5") → 5 |
| float() | str/int → float | float("3.14") → 3.14 |
| str() | int/float → string | str(42) → "42" |
int("3.7") raises an error — you cannot convert a decimal string directly to int. Cast to float first: int(float("3.7")) → 3age ← int(input("Age: ")) — combines input and conversion in one line. Both OCR ERL and Python accept this style.| Char | Code | Notes |
|---|---|---|
| '0' – '9' | 48 – 57 | Digit chars |
| 'A' – 'Z' | 65 – 90 | Uppercase |
| 'a' – 'z' | 97 – 122 | Lowercase |
ASC() takes a single character — not a full string.==, !=, and even < / > (alphabetical order based on ASCII values). Comparison is case-sensitive — "Hello" ≠ "hello".length, substring(), upper(), lower(), ASC(), CHR(), and indexing with [i]. Python methods like find() and replace() only appear in Python-specific questions.word ← "Science"word[2]?word[2] = "i" — strings are 0-indexed, so index 2 is the third character. (1 mark)
word, then output its full length.word[1] instead of word[0] — classic off-by-onestr() when concatenating length with a stringword.upper without the parentheses — in OCR ERL, functions need ()| Operation | OCR ERL | Python |
|---|---|---|
| Length | s.length | len(s) |
| Index | s[i] | s[i] |
| Substring | s.substring(a,n) | s[a:a+n] |
| Uppercase | s.upper() | s.upper() |
| Lowercase | s.lower() | s.lower() |
| ASCII | ASC() / CHR() | ord() / chr() |
word[1] to get the first character. Strings are 0-indexed — the first character is always at index 0. Using index 1 returns the second characterword[0] — last character: word[word.length - 1]word.substring(0, 4) expecting characters 0 to 4 — but the second argument is length, not an end index. substring(0, 4) returns 4 characters starting from 0"Length: " + word.length — this causes an error because length returns an integer and you cannot concatenate a string with an integer using +"Length: " + str(word.length)word.upper() does NOT change word — it returns a new string. After calling word.upper(), the variable word is still in its original caseupper ← word.upper() or use it directly in printstring[i]. Join strings using the + operator — this is concatenationmyString.length or len(myString). The last character is always at index length − 1, not length. Remember: 0-indexed means one less than you might expect[start:end] where end is exclusive (not included)str(), int(), float() to convert between types. You cannot concatenate a string and an integer without castingord() and chr(). Key values: 'A'=65, 'a'=97, '0'=48Get the full resource pack at CSZone.co.uk