4.1.1.7 - Strings, character codes and dates (Python)

4.1.1.7 - Strings, character codes and dates (Python)

Extract and combine text, convert between characters and their codes, and convert numerical and date/time values without losing their meaning.

Length, positions and substrings

A string is a sequence of characters. Python's len(text) gives its length and indexing selects a character using a zero-based position. For text = "ORBIT", the length is 5, text[0] is "O" and text[4] is "T". An empty string has length zero and no valid character index.

Slicing extracts a substring: text[1:4] is "RBI". The start position is included and the stop is excluded. text[:2] is "OR", text[2:] is "BIT", and text[-1] is the last character. A slice may safely be empty or extend past the end; a single out-of-range index raises IndexError. Strings are immutable: make a new string rather than assigning text[0] = "A".

Concatenation joins strings, using +. "gate-" + "A" gives "gate-A". To locate text, text.find("BI") returns 2, the first matching position; .find("X") returns -1 when absent. A result of 0 is a valid match at the beginning, not failure. AQA's “position” can be implemented by indexing or finding a substring as the task demands.

Extract a label

{"code":"label = "ORBIT-27"\nprint(len(label))\nprint(label[0], label[-1])\nprint(label[0:5] + "/" + label[6:])\nprint(label.find("-"))\n","instructions":"Predict all four lines, then Run. Change label to COMET-08, predict/run again, and explain which indices still fit the same five-letter-code format. Then replace [0:5] with [1:5] and predict the missing character before running.","files":[],"original_filename":"main.py","runtime_profile":"python-console-v1","file_mode":"fresh"}

Characters and character codes

A character code is a numerical identifier assigned to a character in an encoding or character set. Python's ord() converts one character to its Unicode code point; chr() converts a valid code point to a one-character string.

Python
letter = "A"
number = ord(letter)
print(number)
print(chr(number + 1))
print(chr(48))

The results are 65, B and 0. Digit characters have consecutive codes 48 through 57; uppercase Latin letters A through Z have codes 65 through 90. Unicode also represents characters outside this small range. A Unicode code point is not automatically the same as a byte in an encoded file; encoding text is a separate operation.

ord("7") is 55, but int("7") is 7. The first obtains the character's identifier; the second interprets a textual numeral. ord() requires exactly one character; chr(65) does not mean “convert the number 65 into the text 65”. A code-shifting task must state its alphabet and boundaries: chr(ord("Z") + 1) is "[", not an automatic wrap to "A".

Converting numerical values and text

Conversion changes a value's representation so it can participate in the required operation. Input returns a string even when the user types digits. int("24") yields an integer, float("2.75") yields a float, and str(24) or str(2.75) produces text for concatenation.

Continue this lesson.

You’re previewing NovaLearn without an account. Create a free account to read the rest.

Create free account