การแมปตัวพิมพ์
กฎสำหรับแปลงอักขระระหว่างตัวพิมพ์ใหญ่ ตัวพิมพ์เล็ก และตัวพิมพ์หัวเรื่อง อาจขึ้นอยู่กับ locale (ปัญหาตัว I ในภาษาตุรกี) และอาจเป็นแบบหนึ่ง-ต่อ-หลาย (ß → SS)
What Is Unicode Case Mapping?
Case mapping is the process of converting a string to its uppercase, lowercase, or titlecase form. While most programmers know str.upper() and str.lower() from ASCII, Unicode case mapping is vastly more complex: it is locale-sensitive, context-sensitive, and sometimes involves one-to-many mappings where a single character maps to multiple characters.
Every cased character in Unicode has three case-mapping properties: Uppercase_Mapping, Lowercase_Mapping, and Titlecase_Mapping. For most characters these are simple one-to-one mappings, but the Unicode Standard specifies special cases that require careful handling.
Notable Special Cases
German ß (LATIN SMALL LETTER SHARP S):
- Lowercase: ß (unchanged)
- Uppercase: SS (two characters!)
- This is a one-to-many mapping: "straße".upper() → "STRASSE"
Turkish/Azerbaijani dotted and dotless I:
- Turkish has four I characters: I (capital, dotless), İ (capital, dotted), ı (small, dotless), i (small, dotted)
- Correct Turkish uppercasing: i → İ (NOT I); ı → I (NOT i)
- Python's default locale-insensitive str.upper() gives the wrong result for Turkish
Greek final sigma ς (U+03C2): - Used only at the end of a word; otherwise σ (U+03CF) is used - Uppercasing: ς → Σ (same as σ) - Lowercasing: Σ → σ or ς depending on position (context-sensitive)
# Simple case mapping
print("hello".upper()) # HELLO
print("HELLO".lower()) # hello
print("hello world".title()) # Hello World
# German ß one-to-many
sharp_s = "straße"
print(sharp_s.upper()) # STRASSE (ß → SS)
print(len(sharp_s)) # 6
print(len(sharp_s.upper())) # 7
# Turkish locale-sensitive (needs locale or explicit mapping)
import locale
# locale.setlocale(locale.LC_ALL, "tr_TR.UTF-8") # system-dependent
dotted_i = "i"
print(dotted_i.upper()) # I (wrong for Turkish)
# unicodedata does NOT do case mapping; use str methods
import unicodedata
# unicodedata.normalize does not change case
# For advanced case folding, use casefold():
print("straße".casefold()) # strasse (case-fold for comparison)
print("STRASSE".casefold()) # strasse
# Case-insensitive comparison (correct approach):
a = "straße"
b = "STRASSE"
print(a.casefold() == b.casefold()) # True
Case Folding vs Case Mapping
Case folding (str.casefold()) is a more aggressive form of lowercasing designed for case-insensitive comparison. It is not appropriate for display. For example, casefold() maps ß → ss and German ü → ü (same), whereas lower() keeps ß as ß.
Quick Facts
| Property | Value |
|---|---|
| Unicode properties | Uppercase_Mapping, Lowercase_Mapping, Titlecase_Mapping |
| Python functions | str.upper(), str.lower(), str.title(), str.casefold() |
| German ß | upper() → SS (one-to-many mapping) |
| Turkish | Requires locale-sensitive mapping for correct İ/ı handling |
| Greek sigma | Context-sensitive: final ς vs medial σ |
| Case folding | str.casefold() — for comparison, not display |
| Spec reference | Unicode Standard Chapter 3.13, SpecialCasing.txt |
คำศัพท์ที่เกี่ยวข้อง
เพิ่มเติมใน คุณสมบัติ
Unicode property (UAX#11) classifying characters as Narrow, Wide, Fullwidth, Halfwidth, Ambiguous, or …
Unicode property controlling how Arabic and Syriac characters connect to adjacent characters. …
Unicode property listing all scripts that use a character, broader than the …
อักขระที่ผู้ใช้รับรู้ได้ — สิ่งที่รู้สึกเหมือนหน่วยเดียว อาจประกอบด้วยหลายจุดรหัส (ฐาน + เครื่องหมายรวม หรือลำดับ emoji ZWJ) 👩💻 = …
การแมปอักขระเป็นส่วนประกอบย่อย การแยกส่วนแบบ canonical รักษาความหมาย (é → e + ́) ในขณะที่การแยกส่วนแบบ compatibility อาจเปลี่ยนความหมาย …
ค่าตัวเลข (0–254) ที่ควบคุมลำดับของเครื่องหมายรวมระหว่างการแยกส่วนแบบ canonical กำหนดว่าเครื่องหมายรวมใดสามารถเรียงลำดับใหม่ได้
ลำดับอักขระสองชุดที่มีเนื้อหาเชิงนามธรรมเดียวกันแต่อาจแตกต่างในรูปลักษณ์ กว้างกว่าความเท่าเทียมแบบ canonical ตัวอย่าง: fi ≈ fi, ² ≈ 2
ลำดับอักขระสองชุดที่มีความหมายเหมือนกันและควรถือว่าเท่าเทียมกัน ตัวอย่าง: é (U+00E9) ≡ e + ◌́ (U+0065 + U+0301)
อักขระที่รูปร่างควรสะท้อนในแนวนอนในบริบท RTL ตัวอย่าง: ( → ), [ → ], { → }, …
เวอร์ชัน Unicode ที่มีการกำหนดอักขระเป็นครั้งแรก มีประโยชน์สำหรับการตรวจสอบการรองรับอักขระในระบบและซอฟต์แวร์เวอร์ชันต่างๆ