Normalize Persian Search Text without Changing Identity
Core idea
Persian and Arabic forms of yeh and kaf have different code points. For a search-only policy you may explicitly map Arabic ي and ك to Persian ی and ک, after NFC. Keep the original value and preserve zero-width non-joiner (ZWNJ, U+200C), which can carry meaningful Persian orthography.
from unicodedata import normalizeoriginal = "كتاب يک"key = normalize("NFC", original).translate(str.maketrans("كي", "کی"))print(key == "کتاب یک")print(original == key)maketrans builds an explicit mapping; translate returns a new string. The stored original is untouched.
Retain a meaningful non-joiner
s = "می\u200cروم"key = s.translate(str.maketrans("كي", "کی"))print("\u200c" in key)print(key == s)This mapping changes only the two chosen letters; the non-joiner remains.
Common mistake
Do not strip all format characters or normalize account IDs silently. Letter mapping is an application search policy, not canonical Unicode equivalence; handle collisions and preserve originals.
Practice
Build search keys for Arabic/Persian letter variants, and check that ZWNJ and untouched text remain unchanged.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Persian search keys
Check your understanding of persian text processing.
What is printed?
print("ي" == "ی")What is printed?
s="می\u200cروم"print("\u200c" in s.translate(str.maketrans("كي", "کی")))What is the safest treatment of account identifiers?
Is Arabic-to-Persian letter mapping canonical normalization?