Guided pathThis is part of Understand Transformers & BERTBack to the path

Find-and-replace across many lines

P42.from-paper-to-notebook.03 · Audience: guest, language-pro, it-ml · Prerequisites: Build a vocabulary table

Real LLM grading for this pageLLM grading (this page):

A term was renamed, a client became a customer, a spelling was standardised — and now it must change everywhere. In a document you would scroll through and fix each line by hand, hoping to miss none. The notebook applies the one rule to the whole list at once: a loop over the lines and .replace on each.

Step 1 / 3 — replace swaps a word in a string
ⓘ Concept: .replace hands back a fixed copy
line.replace(OLD, NEW) returns a new string with every OLD swapped for NEW. The original line is left exactly as it was — replace hands you a fresh copy rather than editing in place, so nothing is lost.

Why it matters — Knowing that replace returns a new string — and never touches the original — is what makes it safe to run across a whole document.

Ask the mentor about this module

Ask a question about this content. The mentor explains and grounds its answer in what you are studying; asking is recorded as a learning signal, not a grade.

Images, PDF or text. Kept on this device only.
Keeping your files on this device

Off by default. The mentor always gets your file; this only decides whether your own copy stays here. Copies live in this browser only - they do not follow you to another device, and clearing site data removes them.

Ctrl/Cmd + Enter to send

🎓 Practice ladder

1 graded rung · ~6 min

Your turn: loop over the lines and build a new list with the old word replaced by the new one on every line. Press Run.

Explorer rung — find-and-replace across lines

Loading exercise…

My notes on this module

Loading your notes...

Where next?

Continue

Automate your own task →

From Paper to Notebook · P42

Later in From Paper to Notebook

Find-and-replace across many lines — TransformerLab