Skip to content

Commit 4c9895a

Browse files
committed
Clarify Unicode string baseline
1 parent 1a9afa7 commit 4c9895a

6 files changed

Lines changed: 51 additions & 37 deletions

File tree

‎docs/example-quality-rubric.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Score each example on a 10 point scale:
1818

1919
Topic-specific gates:
2020

21-
- **Text and strings** — examples must distinguish text from bytes, explain Unicode/code-point behavior when relevant, and show the boundary where encoding such as UTF-8 appears. A string page that only demonstrates ASCII operations is incomplete.
21+
- **Text and strings** — examples must distinguish text from bytes, explain Unicode/code-point behavior when relevant, and show the boundary where encoding such as UTF-8 appears. A string page that only demonstrates ASCII operations is incomplete. When using non-English text to show that code points and bytes differ, include an English/ASCII baseline with the same kind of phrase so the contrast is visible rather than implied.
2222

2323
Release gates outside the score:
2424

@@ -73,4 +73,4 @@ Before publishing or substantially editing an example, ask:
7373
8. What neighboring feature would a learner confuse this with, and does the page explain the boundary?
7474
9. Does the data shape explain why this feature exists?
7575
10. What syntax form would disappear from the catalog if this page were removed, and is that covered somewhere else?
76-
11. For text examples, does the page make Unicode and encoding boundaries visible instead of assuming ASCII-only strings?
76+
11. For text examples, does the page make Unicode and encoding boundaries visible instead of assuming ASCII-only strings? If non-English text is used, is it compared with an English/ASCII baseline?

‎docs/research-similar-sites.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,6 @@ String/Text lesson observed across example sites:
3838
- Learn X By Example's Ruby string page also uses Thai text to contrast character length with UTF-8 bytes.
3939
- Nim By Example explicitly warns that Unicode text may require specialized handling beyond byte-like string operations.
4040

41-
Implication for Python By Example: string examples should not be ASCII-only. They should show `str` as Unicode text, contrast it with `bytes`, and make UTF-8 encoding visible at I/O boundaries.
41+
Implication for Python By Example: string examples should not be ASCII-only. They should show `str` as Unicode text, contrast it with `bytes`, and make UTF-8 encoding visible at I/O boundaries. When a non-English word demonstrates multi-byte encoding, pair it with an English/ASCII word of the same role so readers can see the baseline instead of inferring it.
4242

4343
Positioning for this project: keep Go By Example's compact language tour, borrow Rust/Kotlin's expectation that examples can run, preserve Python-doc authority, and avoid course-like sprawl.

‎src/asset_manifest.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# Generated by scripts/fingerprint_assets.py. Do not edit by hand.
22
ASSET_PATHS = {'SITE_CSS': '/site.06e133dcde6a.css', 'SYNTAX_JS': '/syntax-highlight.3b6c7f730d46.js', 'EDITOR_JS': '/editor.dd81f5171b14.js'}
3-
HTML_CACHE_VERSION = '8fe1cc517dc0'
3+
HTML_CACHE_VERSION = '55a58454dcc0'

‎src/example_sources/strings.md‎

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,22 @@ summary = "Strings are immutable Unicode text sequences."
66
doc_path = "/library/stdtypes.html#text-sequence-type-str"
77
+++
88

9-
Python strings are immutable Unicode text sequences. A `str` stores text as Unicode code points, so it can represent Thai, accented letters, emoji, and ordinary ASCII with the same type.
9+
Python strings are immutable Unicode text sequences. A `str` stores text as Unicode code points, so it can represent English, Thai, accented letters, emoji, and ordinary ASCII with the same type.
1010

11-
This is different from bytes. Use `str` when you mean text, and encode to `bytes` only at boundaries such as files, network protocols, and binary APIs.
11+
Unicode matters because text length and byte length are different questions. The English word `"hello"` uses five code points and five UTF-8 bytes because ASCII characters encode as one byte each. The Thai greeting `"สวัสดี"` has six code points but needs eighteen UTF-8 bytes.
1212

13-
String operations such as `upper()` and `strip()` return new strings instead of changing the original. Use indexing and iteration for code points, and `encode()` when you need the underlying UTF-8 bytes.
13+
Use `str` when you mean text, and encode to `bytes` only at boundaries such as files, network protocols, and binary APIs. String operations such as `upper()` and `strip()` return new strings instead of changing the original.
1414

1515
:::program
1616
```python
17-
word = "สวัสดี"
18-
print(len(word))
19-
print(len(word.encode("utf-8")))
20-
print(word[0])
21-
print([hex(ord(char)) for char in word[:2]])
17+
english = "hello"
18+
thai = "สวัสดี"
19+
20+
for label, word in [("English", english), ("Thai", thai)]:
21+
print(label, word, len(word), len(word.encode("utf-8")))
22+
23+
print(thai[0])
24+
print([hex(ord(char)) for char in thai[:2]])
2225

2326
text = " café "
2427
clean = text.strip()
@@ -29,26 +32,28 @@ print(clean.encode("utf-8"))
2932
:::
3033

3134
:::cell
32-
A Python `str` is text, not raw bytes. `len()` counts Unicode code points, while UTF-8 encoding shows how many bytes are needed at a byte boundary.
35+
Compare an English greeting with a Thai greeting. Both are Python `str` values, but UTF-8 uses one byte for each ASCII code point and multiple bytes for many non-ASCII code points.
3336

3437
```python
35-
word = "สวัสดี"
36-
print(len(word))
37-
print(len(word.encode("utf-8")))
38+
english = "hello"
39+
thai = "สวัสดี"
40+
41+
for label, word in [("English", english), ("Thai", thai)]:
42+
print(label, word, len(word), len(word.encode("utf-8")))
3843
```
3944

4045
```output
41-
6
42-
18
46+
English hello 5 5
47+
Thai สวัสดี 6 18
4348
```
4449
:::
4550

4651
:::cell
47-
Indexing and iteration work with Unicode code points. `ord()` returns the integer code point, which is often displayed in hexadecimal when teaching text encoding.
52+
Indexing and iteration work with Unicode code points, not encoded bytes. `ord()` returns the integer code point, which is often displayed in hexadecimal when teaching text encoding.
4853

4954
```python
50-
print(word[0])
51-
print([hex(ord(char)) for char in word[:2]])
55+
print(thai[0])
56+
print([hex(ord(char)) for char in thai[:2]])
5257
```
5358

5459
```output
@@ -78,6 +83,7 @@ b'caf\xc3\xa9'
7883
:::note
7984
- Use `str` for text and `bytes` for binary data.
8085
- `len(text)` counts Unicode code points; `len(text.encode("utf-8"))` counts encoded bytes.
86+
- ASCII text is a useful baseline because each ASCII code point is one UTF-8 byte.
8187
- String methods return new strings because strings are immutable.
8288
- User-visible “characters” can be more subtle than code points; combining marks and emoji sequences may need specialized text handling.
8389
:::

‎src/example_sources_data.py‎

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

‎tests/fixtures/golden_examples.py‎

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -434,33 +434,41 @@
434434
'section': 'Text',
435435
'summary': 'Strings are immutable Unicode text sequences.',
436436
'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'
442445
'\n'
443446
'text = " café "\n'
444447
'clean = text.strip()\n'
445448
'print(clean)\n'
446449
'print(clean.upper())\n'
447450
'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",
449452
'notes': ['Use `str` for text and `bytes` for binary data.',
450453
'`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.',
451455
'String methods return new strings because strings are immutable.',
452456
'User-visible “characters” can be more subtle than code points; combining marks and emoji sequences may '
453457
'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',
458466
'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]])',
462470
'output': "ส\n['0xe2a', '0xe27']",
463-
'line': 32},
471+
'line': 34},
464472
{'prose': ['String methods return new strings because strings are immutable. Encoding turns text into '
465473
'bytes when another system needs a byte representation.'],
466474
'code': 'text = " café "\n'
@@ -469,7 +477,7 @@
469477
'print(clean.upper())\n'
470478
'print(clean.encode("utf-8"))',
471479
'output': "café\nCAFÉ\nb'caf\\xc3\\xa9'",
472-
'line': 46}]},
480+
'line': 48}]},
473481
{'slug': 'string-formatting',
474482
'title': 'String Formatting',
475483
'section': 'Text',

0 commit comments

Comments
 (0)