s.translate(str.maketrans('aeiou', '12345'))
String & Text Processing

Meaning

The call creates a translation table with str.maketrans and applies it to a string using str.translate, allowing bulk character-to-character mapping in a single operation. This avoids the need for multiple successive str.replace calls, which can be slower and more verbose. It is used when you need to replace or delete several distinct characters throughout a string at once.

Primary Function

String manipulation

Communicative Purpose

Replace or delete multiple characters in a string efficiently.

Pattern

string.translate(str.maketrans(source, target))

Core Structure

... .translate(str.maketrans(... , ...))

Função primária

String manipulation

Propósito comunicativo

Replace or delete multiple characters in a string efficiently.

Situações de gatilho

Data cleaning: removing punctuation from CSV fields; Web scraping: normalizing extracted text by converting accented characters to plain ASCII; Log processing: stripping control characters from log lines

Contextos

General‑purpose Python scripts, data‑cleaning pipelines, text‑processing utilities, web‑scraping code.

Padrão

string.translate(str.maketrans(source, target))

Estrutura central

... .translate(str.maketrans(... , ...))

Slots de substituição

string_var: identifier, src_chars: str, dst_chars: str

Colocados típicos

  • str.maketrans
  • translate
  • mapping table
  • None (for deletions)

Substituições comuns

  • Chaining multiple str.replace calls
  • using regular expressions with re.sub
  • building a dict and using translate with that dict.

Erros comuns

Forgetting that str.translate returns a new string (strings are immutable), providing mismatched source and destination lengths, passing a dict instead of a translation table created by str.maketrans, or attempting in‑place modification.

Similar / contraste

str.replace replaces one substring at a time and is less efficient for many characters; regex re.sub can handle patterns but adds overhead and complexity.

Interferências

Coming from JavaScript: String.replace does not modify the original string and works with regex; coming from C: tr may modify the buffer in‑place, which is not the case in Python.

Família do chunk

  • string manipulation
  • character mapping
  • text cleaning

Nuance

translate works on single Unicode code points; it cannot replace multi‑character sequences. Use it only when mapping individual characters.

Efeito pragmático

Provides a concise, high‑performance way to perform bulk character replacements or deletions, reducing the need for multiple replace calls.

Dica de memória

Vowels → numbers via translate

Nota

str.translate operates on single Unicode code points; it cannot replace multi-character sequences. For those, use regex or chained replace.

Upgrade path

Use a regular expression with re.sub for pattern‑based replacements or combine translate with Unicode category tables for locale‑aware transformations.

Frequência: MediumFormulaicidade: Semi-fixedTipo de construção: method callPrioridade de aquisição: Recognition firstPrioridade de output: BothTag de espaçamento: Short-term

Log in to save chunks.