text.partition
String & Text Processing

Meaning

The `text.partition(sep)` method splits a string at the first occurrence of the separator and returns a three‑element tuple (before, separator, after). It solves the pain point of needing to keep the delimiter while extracting surrounding parts, which `split()` discards. You reach for it whenever you have a known delimiter and must reliably obtain the surrounding substrings without losing the delimiter.

Primary Function

String manipulation

Communicative Purpose

Enables extracting the part before, the separator, and the part after a specific delimiter without losing the delimiter.

Pattern

source.partition(delimiter)

Core Structure

... .partition(...)

Função primária

String manipulation

Propósito comunicativo

Enables extracting the part before, the separator, and the part after a specific delimiter without losing the delimiter.

Situações de gatilho

Log parsing: separating timestamp from message using a known delimiter; File path handling: splitting directory and filename at the last '/' character

Contextos

General Python scripts, data‑processing pipelines, command‑line utilities, web back‑ends that manipulate textual data

Padrão

source.partition(delimiter)

Estrutura central

... .partition(...)

Slots de substituição

source: str, delimiter: str

Colocados típicos

  • split()
  • rsplit()
  • join()
  • replace()
  • find()

Substituições comuns

  • Using `str.split(delimiter
  • 1)` and then re‑joining parts – more verbose and loses the delimiter
  • Using a regular expression with `re.search` – flexible but slower and harder to read

Erros comuns

Assuming `partition` returns only two parts – leads to unpacking errors because it always returns three elements. Using a separator that never occurs – results in (original_string, '', '') which may be misinterpreted as a successful split. Neglecting to handle the empty middle element when the separator is at the start or end – can cause logic that expects a non‑empty separator to fail.

Similar / contraste

`str.split(sep)` – returns a list of all parts and discards the separator. `str.rpartition(sep)` – similar but starts splitting from the rightmost occurrence. `str.find(sep)` – returns index only; you must slice manually.

Interferências

Coming from JavaScript: using `split()` expecting three parts → JavaScript `split` returns an array without the delimiter, unlike Python `partition` which preserves it.

Família do chunk

  • string methods
  • text processing

Nuance

Do not use when the separator may appear multiple times and you need all splits; `partition` only handles the first occurrence. Performance is O(n) scanning once, comparable to `split`, but avoids the overhead of building a list of all parts. If the separator is an empty string, `partition` raises a ValueError – ensure the delimiter is non‑empty.

Efeito pragmático

Provides a safe, one‑step way to separate a string while keeping the delimiter, reducing bugs in parsers and simplifying downstream processing.

Dica de memória

Think of `partition` as a three‑way traffic light that separates the road before the signal, the signal itself, and the road after it.

Nota

`partition` never raises an exception; if the separator is absent it returns the original string and two empty strings.

Frequência: HighFormulaicidade: FixedTipo de construção: method_callPrioridade de aquisição: Automatic productionPrioridade de output: BothTag de espaçamento: Immediate

Log in to save chunks.