Exercise 2: A Genuinely Empty Text Field Round-Trips Correctly — Possible Solution ==================================================================== THE TEST ------------------------------ schema4 = Schema([('note', 'TEXT')]) row4 = [""] encoded4 = encode_row(schema4, row4) decoded4 = decode_row(schema4, encoded4) RESULT ------------------------------ encoded4 == b'\x00\x00' (exactly 2 bytes) decoded4 == [""] -- exact match HOW MANY BYTES AN EMPTY STRING ACTUALLY PRODUCES ------------------------------ encode_text("") computes: encoded = "".encode('utf-8') # b'' -- zero bytes, correctly length_prefix = struct.pack('>H', len(encoded)) # struct.pack('>H', 0) -> b'\x00\x00' return length_prefix + encoded # b'\x00\x00' + b'' = b'\x00\x00' The final record is exactly 2 bytes long: the length prefix itself (storing the value 0), with zero content bytes following it, since there's genuinely nothing to store for the text itself. WHY THIS IS A VALID RECORD, NOT A SPECIAL CASE ------------------------------ Nothing in encode_text or decode_text branches on whether the string happens to be empty -- the same code path that handles a 5-character string handles a 0-character one, simply because len(b'') is a perfectly ordinary, valid value (0) for the '>H' format to pack, and data[offset:offset+0] is a perfectly ordinary, valid Python slice that returns an empty bytes object. decode_text reads the length (0), reads exactly 0 bytes starting at the current offset (getting b'' back), and decodes that empty byte string to "" -- again, no special casing anywhere; Python's own b''.decode('utf-8') is simply defined to return "". WHY THIS WORKS AS AN ANSWER ------------------------------ This confirms the length-prefix design handles zero-length content as a natural consequence of its own general mechanism, not as something that needed to be anticipated and coded around separately. A real database has to store empty strings constantly (an optional "notes" field a user simply left blank, for instance) -- a record format that required special-casing "what if this column has no content at all" would be a design with a real gap in it; this one doesn't have that gap, because the length prefix already generalizes correctly down to zero.