Algorithmspattern transformation
Pattern Transformation (translate, pipelines, replace)
TT
Testlaa Team
May 14, 2026•1 min read
Pattern transformation maps whole strings through substitutions—character maps (str.translate), multi-step pipelines, or regex replacements. Think declaratively: what is the alphabet before and after?
Why this shows up in the real world
Romanization systems map Devanagari to Latin letters by table. PII redaction replaces digit runs with * while preserving length metadata.
Core idea (explained for students)
Use str.maketrans for one-to-one char maps. For whole-word swaps, re.sub with a callback keeps logic localized.
Try this in Python
def rot13(s: str) -> str:
import codecs
return codecs.encode(s, "rot13")
print(rot13("uryyb"))
Common mistakes
- Collisions when two source chars map to the same target unintentionally.
- Chained replaces where order matters (
ab→xthenba→y).
Key takeaways
- Prefer immutable transforms (return new string) over in-place mutation lists unless performance requires buffers.
- Snapshot inputs before multi-pass transforms for debugging.
Tags:
StringsPythonStudents
