Swap a specific token inside every string in a column. By hand, call s.replace(old, new) on each element. With pandas, Series.str.replace (old, new, regex=False) applies the literal substitution column-wide.

By hand

Loop over strings and call Python's built-in .replace('St.', 'Street') on each. Strings without the token are returned unchanged. The trace shows result filling with the expanded street names.

naive.py
Replay: real traced execution (multi-file project)
strings = ['Oak St.', 'Pine St.', 'Elm Ave', 'Maple St.', 'Cedar Ave']
result = []
for s in strings:
    result.append(s.replace('St.', 'Street'))
print('RESULT:', result)
  1. strings ← ['Oak St.', 'Pine St.', 'Elm Ave', 'Maple St.', 'Cedar Ave']

    1strings = ['Oak St.', 'Pine St.', 'Elm Ave', 'Maple St.', 'Cedar Ave']2result = []
    values this step['Oak St.', 'Pine St.', 'Elm Ave', 'Maple St.', 'Cedar Ave']strings
  2. result ← []

    1strings = ['Oak St.', 'Pine St.', 'Elm Ave', 'Maple St.', 'Cedar Ave']2result = []3for s in strings:
    values this step[]result
  3. s ← 'Oak St.'

    2result = []3for s in strings:4    result.append(s.replace('St.', 'Street'))
    values this step'Oak St.'s
  4. result ← ['Oak Street']

    3for s in strings:4    result.append(s.replace('St.', 'Street'))5print('RESULT:', result)
    values this step[] ['Oak Street']result
  5. s ← 'Pine St.'

    2result = []3for s in strings:4    result.append(s.replace('St.', 'Street'))
    values this step'Oak St.' 'Pine St.'s
  6. result ← ['Oak Street', 'Pine Street']

    3for s in strings:4    result.append(s.replace('St.', 'Street'))5print('RESULT:', result)
    values this step['Oak Street'] ['Oak Street', 'Pine Street']result
  7. s ← 'Elm Ave'

    2result = []3for s in strings:4    result.append(s.replace('St.', 'Street'))
    values this step'Pine St.' 'Elm Ave's
  8. result ← ['Oak Street', 'Pine Street', 'Elm Ave']

    3for s in strings:4    result.append(s.replace('St.', 'Street'))5print('RESULT:', result)
    values this step['Oak Street', 'Pine Street'] ['Oak Street', 'Pine Street', 'Elm Ave']result
  9. s ← 'Maple St.'

    2result = []3for s in strings:4    result.append(s.replace('St.', 'Street'))
    values this step'Elm Ave' 'Maple St.'s
  10. result ← ['Oak Street', 'Pine Street', 'Elm Ave', 'Maple Street']

    3for s in strings:4    result.append(s.replace('St.', 'Street'))5print('RESULT:', result)
    values this step['Oak Street', 'Pine Street', 'Elm Ave'] ['Oak Street', 'Pine Street', 'Elm Ave', 'Maple Street']result
  11. s ← 'Cedar Ave'

    2result = []3for s in strings:4    result.append(s.replace('St.', 'Street'))
    values this step'Maple St.' 'Cedar Ave's
  12. result ← ['Oak Street', 'Pine Street', 'Elm Ave', 'Maple Street', 'Cedar Ave']

    3for s in strings:4    result.append(s.replace('St.', 'Street'))5print('RESULT:', result)
    values this step['Oak Street', 'Pine Street', 'Elm Ave', 'Maple Street'] ['Oak Street', 'Pine Street', 'Elm Ave', 'Maple Street', 'Cedar Ave']result
  13. for s in strings:

    2result = []3for s in strings:4    result.append(s.replace('St.', 'Street'))
  14. stdout ← RESULT: ['Oak Street', 'Pine Street', 'Elm Ave', 'Maple Street', 'Cedar Ave']

    4    result.append(s.replace('St.', 'Street'))5print('RESULT:', result)
    values this stepRESULT: ['Oak Street', 'Pine Street', 'Elm Ave', 'Maple Street', 'Cedar Ave']stdout

With pandas

df['x'].str.replace('St.', 'Street', regex=False) performs the same literal substitution on every element. regex=False makes the pattern a plain string — the default in pinned pandas, but explicit here because 'St.' contains a dot that would match any character as a regex.

library.py
import pandas as pd
from dalib.display import set_display
set_display()

strings = ['Oak St.', 'Pine St.', 'Elm Ave', 'Maple St.', 'Cedar Ave']
df = pd.DataFrame({'x': strings})
s = df['x'].str.replace('St.', 'Street', regex=False)
result = s.tolist()
print('index:', s.index.tolist())
print('dtype:', s.dtype)
print('values:', s.tolist())
print('RESULT:', result)
index: [0, 1, 2, 3, 4]
dtype: object
values: ['Oak Street', 'Pine Street', 'Elm Ave', 'Maple Street', 'Cedar Ave']
RESULT: ['Oak Street', 'Pine Street', 'Elm Ave', 'Maple Street', 'Cedar Ave']

Implementation notes

  • regex=False is the default and treats the pattern as a literal string. Pass regex=True only when you need a regex pattern (e.g. a character class or quantifier).
  • Python's str.replace replaces every non-overlapping occurrence within each string, as does the pandas accessor — not just the first one.
  • Cross-reference: parse-formatted-numbers (ch02) for the same .str.replace pattern applied before a numeric cast.