str.translate() Translate chars
Last updated: 2026-09-22
str.translate() Translate chars
str.translate(table) translates chars using table (typically built with str.maketrans).
| Category | String Methods |
|---|---|
| Kind | Built-in Type Method |
| Python Version | all |
📝 Syntax
str.translate(table)
⚙️ Parameters
| table Required | A translation table, typically built by str.maketrans(). |
|---|
Returns:str. A copy with chars replaced or removed according to the table.
Mutates the original:No (returns a new object)
💥 Raises
TypeError— Raised when table is not a mapping/indexable object.
▶ Example
Edit the code, press Run, and see the result in the output panel:
Output:
hll wrld
💡 Related Cases
More case studies coming soon — check back later.
❓ FAQ
QWhat argument does translate() require?
AA character-mapping translation table, usually generated by str.maketrans(). For example tbl = str.maketrans('aeiou', '12345'), then s.translate(tbl).
QCan it delete characters?
AYes. The third argument of maketrans specifies characters to delete: str.maketrans('', '', 'aeiou') combined with translate() can remove all vowels.
QWhat is the difference from replace()?
Atranslate() replaces or deletes characters one at a time via a character table, handling multiple character groups in one pass without overlapping matches; replace() replaces substrings. For mass single-character substitution, translate() is faster.