|
434 | 434 | 'section': 'Text', |
435 | 435 | 'summary': 'Strings are immutable Unicode text sequences.', |
436 | 436 | 'doc_url': 'https://docs.python.org/3.13/library/stdtypes.html#text-sequence-type-str', |
437 | | - 'code': 'word = "สวัสดี"\n' |
438 | | - 'print(len(word))\n' |
439 | | - 'print(len(word.encode("utf-8")))\n' |
440 | | - 'print(word[0])\n' |
441 | | - 'print([hex(ord(char)) for char in word[:2]])\n' |
| 437 | + 'code': 'english = "hello"\n' |
| 438 | + 'thai = "สวัสดี"\n' |
| 439 | + '\n' |
| 440 | + 'for label, word in [("English", english), ("Thai", thai)]:\n' |
| 441 | + ' print(label, word, len(word), len(word.encode("utf-8")))\n' |
| 442 | + '\n' |
| 443 | + 'print(thai[0])\n' |
| 444 | + 'print([hex(ord(char)) for char in thai[:2]])\n' |
442 | 445 | '\n' |
443 | 446 | 'text = " café "\n' |
444 | 447 | 'clean = text.strip()\n' |
445 | 448 | 'print(clean)\n' |
446 | 449 | 'print(clean.upper())\n' |
447 | 450 | 'print(clean.encode("utf-8"))\n', |
448 | | - 'expected_output': "6\n18\nส\n['0xe2a', '0xe27']\ncafé\nCAFÉ\nb'caf\\xc3\\xa9'\n", |
| 451 | + 'expected_output': "English hello 5 5\nThai สวัสดี 6 18\nส\n['0xe2a', '0xe27']\ncafé\nCAFÉ\nb'caf\\xc3\\xa9'\n", |
449 | 452 | 'notes': ['Use `str` for text and `bytes` for binary data.', |
450 | 453 | '`len(text)` counts Unicode code points; `len(text.encode("utf-8"))` counts encoded bytes.', |
| 454 | + 'ASCII text is a useful baseline because each ASCII code point is one UTF-8 byte.', |
451 | 455 | 'String methods return new strings because strings are immutable.', |
452 | 456 | 'User-visible “characters” can be more subtle than code points; combining marks and emoji sequences may ' |
453 | 457 | 'need specialized text handling.'], |
454 | | - 'cells': [{'prose': ['A Python `str` is text, not raw bytes. `len()` counts Unicode code points, while UTF-8 ' |
455 | | - 'encoding shows how many bytes are needed at a byte boundary.'], |
456 | | - 'code': 'word = "สวัสดี"\nprint(len(word))\nprint(len(word.encode("utf-8")))', |
457 | | - 'output': '6\n18', |
| 458 | + 'cells': [{'prose': ['Compare an English greeting with a Thai greeting. Both are Python `str` values, but UTF-8 uses ' |
| 459 | + 'one byte for each ASCII code point and multiple bytes for many non-ASCII code points.'], |
| 460 | + 'code': 'english = "hello"\n' |
| 461 | + 'thai = "สวัสดี"\n' |
| 462 | + '\n' |
| 463 | + 'for label, word in [("English", english), ("Thai", thai)]:\n' |
| 464 | + ' print(label, word, len(word), len(word.encode("utf-8")))', |
| 465 | + 'output': 'English hello 5 5\nThai สวัสดี 6 18', |
458 | 466 | 'line': 17}, |
459 | | - {'prose': ['Indexing and iteration work with Unicode code points. `ord()` returns the integer code point, ' |
460 | | - 'which is often displayed in hexadecimal when teaching text encoding.'], |
461 | | - 'code': 'print(word[0])\nprint([hex(ord(char)) for char in word[:2]])', |
| 467 | + {'prose': ['Indexing and iteration work with Unicode code points, not encoded bytes. `ord()` returns the ' |
| 468 | + 'integer code point, which is often displayed in hexadecimal when teaching text encoding.'], |
| 469 | + 'code': 'print(thai[0])\nprint([hex(ord(char)) for char in thai[:2]])', |
462 | 470 | 'output': "ส\n['0xe2a', '0xe27']", |
463 | | - 'line': 32}, |
| 471 | + 'line': 34}, |
464 | 472 | {'prose': ['String methods return new strings because strings are immutable. Encoding turns text into ' |
465 | 473 | 'bytes when another system needs a byte representation.'], |
466 | 474 | 'code': 'text = " café "\n' |
|
469 | 477 | 'print(clean.upper())\n' |
470 | 478 | 'print(clean.encode("utf-8"))', |
471 | 479 | 'output': "café\nCAFÉ\nb'caf\\xc3\\xa9'", |
472 | | - 'line': 46}]}, |
| 480 | + 'line': 48}]}, |
473 | 481 | {'slug': 'string-formatting', |
474 | 482 | 'title': 'String Formatting', |
475 | 483 | 'section': 'Text', |
|
0 commit comments