str.maketrans()
Build a dictionary for str.translate; this static method does not transform text itself. The one-argument form accepts a dictionary whose keys are integer code points or one-character strings; string keys become integer ordinals. Values can be code points, strings of any length, or None for deletion. In the two-string form, characters at corresponding positions are paired; unequal lengths raise ValueError. The optional third string marks characters for deletion, overriding earlier substitutions for those characters. Repeated source characters use the last pairing. Table construction is not comprehensive validation of every replacement value; invalid values can fail when translate uses them.
Formal Syntax & Structure
str.maketrans(mapping, /)str.maketrans(from_chars, to_chars, remove='', /)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| mapping_or_from_chars | dict | str | Dictionary of character mappings, or source string when a second string is provided. |
| to_chars | str | Destination characters, equal length to the source string. |
| remove | str | Characters to delete; optional with the two-string form. |
Return Value
Integer-keyed translation table; not translated text.
Working Example
table = str.maketrans("يك", "یک", "!")print(table[ord("ي")] == ord("ی"))print(table[ord("!")] is None)print("يك كتاب!".translate(table))expanded = str.maketrans({"&": " and ", "#": None})print("A&B#".translate(expanded))