Case Folding
Mapping characters to a common case form for case-insensitive comparison. More comprehensive than lowercasing: German ß → ss, Turkish İ → i (with locale considerations).
What is Case Folding?
Case folding is a Unicode operation that converts text to a form suitable for case-insensitive comparison. It is defined in the Unicode Standard and supported by the CaseFolding.txt data file in the Unicode Character Database. Case folding is closely related to, but distinct from, simple lowercasing: while lowercasing converts a string to its lowercase representation for display, case folding converts a string to a canonical form specifically optimized for string comparison regardless of case.
The practical difference is that case folding handles edge cases that simple lowercasing misses — particularly in languages with complex case mapping behavior.
CaseFolding.txt: The Data File
The Unicode Consortium publishes CaseFolding.txt as part of the Unicode Character Database. It maps each character to its case-folded form using one of four status codes:
| Status | Meaning |
|---|---|
| C (Common) | Safe for all contexts; included in both simple and full folding |
| F (Full) | Full case folding only; maps one character to multiple characters |
| S (Simple) | Simple case folding only; maps one character to one character |
| T (Turkic) | Special folding for Turkic languages (replaces C/S mappings) |
Simple vs. Full Case Folding
Simple case folding maps every character to at most one character — a one-to-one mapping. It is suitable for environments where string length must be preserved.
Full case folding allows one character to map to a sequence of multiple characters. The classic example is the German sharp S:
- ß (U+00DF, Latin Small Letter Sharp S)
- Simple fold: ß → ß (no change — no uppercase in simple mapping)
- Full fold: ß → ss (two characters)
This means that a full case-fold comparison of "STRASSE" and "Straße" would correctly identify them as equal (both fold to "strasse"), while a simple lowercase comparison would not.
# Python uses full case folding via str.casefold()
"STRASSE".casefold() == "Straße".casefold() # True
"STRASSE".lower() == "Straße".lower() # False
# The key difference
"Straße".casefold() # "strasse"
"Straße".lower() # "straße" ← ß preserved
Python's str.casefold() implements full Unicode case folding, while str.lower() implements Unicode simple lowercasing.
Locale-Sensitive Folding: The Turkish Problem
The most significant locale-specific case folding issue involves the Turkish and Azerbaijani I. In most languages:
- Uppercase I → lowercase i
- Uppercase İ does not exist (or is rare)
In Turkish and Azerbaijani: - Uppercase İ (U+0130, Latin Capital Letter I with Dot Above) → lowercase i (U+0069) - Uppercase I (U+0049, Latin Capital Letter I) → lowercase ı (U+0131, Latin Small Letter Dotless I)
The T (Turkic) status entries in CaseFolding.txt provide the Turkic-specific mappings. Standard Unicode case folding without the T entries is incorrect for Turkish text: it would map I → i rather than I → ı, causing "KISA" and "kısa" (meaning "short") to compare as unequal while "KISA" and "kisa" would compare as equal — the wrong result.
# Correct Turkish case comparison requires locale awareness
import locale
# Python's str.casefold() uses C-locale folding (non-Turkic)
# For Turkish: use icu-python or a locale-aware library
How Case Folding Differs from Lowercasing
| Operation | Purpose | Handles ß→ss | Handles Turkish İ | String length |
|---|---|---|---|---|
str.lower() |
Display (lowercase) | No (ß→ß) | No (I→i) | Preserved |
str.casefold() |
Comparison | Yes (ß→ss) | No | May increase |
| Turkic case fold | Comparison in TR/AZ | Yes | Yes | May increase |
Quick Facts
| Property | Value |
|---|---|
| Data file | CaseFolding.txt in Unicode Character Database |
| Status codes | C (Common), F (Full), S (Simple), T (Turkic) |
| Key difference from lower() | Full folding expands ß→ss |
| Turkish exception | I→ı and İ→i (T status entries) |
| Python simple fold | str.lower() |
| Python full fold | str.casefold() |
| Use case | Case-insensitive string comparison and search |
관련 용어
알고리즘의 더 많은 용어
Rules (UAX#29) for determining where one user-perceived character ends and another begins. …
정규화 형식 C: 분해 후 정규 재합성하여 가장 짧은 형식을 생성합니다. 데이터 …
정규화 형식 D: 재합성 없이 완전히 분해합니다. macOS HFS+ 파일 시스템에서 사용됩니다. …
정규화 형식 KC: 호환 분해 후 정규 합성. 시각적으로 유사한 문자를 통합합니다(fi→fi, …
정규화 형식 KD: 재합성 없이 호환 분해. 가장 강력한 정규화 방식으로 서식 …
Comparing Unicode strings requires normalization (NFC/NFD) and optionally collation (locale-aware sorting). Binary …
유니코드 단어 경계 규칙에 따라 결정된 단어 사이의 위치. 단순히 공백으로 분리하는 …
유니코드 규칙에 따른 문장 사이의 위치. 마침표로만 분리하는 것보다 복잡하며, 약어(Mr.), 생략 …
문자 양방향 범주와 명시적 방향 재정의를 사용하여 혼합 방향 텍스트(예: 영어 + …
유니코드 텍스트를 표준 정규 형식으로 변환하는 과정. 네 가지 형식: NFC(합성), NFD(분해), …