str.translate()
Transform each original Unicode code point using integer-key lookup on the supplied table. A returned integer denotes a replacement code point, a string allows expansion (or an empty replacement), and None deletes the character. A LookupError during lookup leaves that character unchanged. Other errors can propagate; invalid replacement types or out-of-range integers raise errors when used. Replacements are not recursively translated. This is character mapping, not substring replacement or language translation. Persian letter harmonization can use explicit mappings, but that is not full Unicode normalization and may be unsuitable when the original spelling matters. The source string remains unchanged.
Formal Syntax & Structure
text.translate(table, /)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| table | object supporting __getitem__ | Lookup by integer Unicode code point, typically the dictionary from str.maketrans. |
Return Value
Text with mapped, expanded, deleted and unchanged characters.
Working Example
text = "يك كتاب!"table = str.maketrans({"ي": "ی", "ك": "ک", "!": None})print(text.translate(table))print(text)print("A&B".translate({ord("&"): " and "}))print("ab".translate(str.maketrans("ab", "bc")))