Remove leading and trailing whitespace from each string in a column. By hand, call s.strip() on each element in a loop. With pandas, Series.str.strip() applies the same operation across the entire column at once.

By hand

Loop over strings and call .strip() on each, appending the cleaned value to result. The trace shows spaces disappearing from each element as the list grows.

naive.py
Replay: real traced execution (multi-file project)
strings = ['  Alice  ', 'Bob ', '  Carol', 'Dave', '  Eve  ']
result = []
for s in strings:
    result.append(s.strip())
print('RESULT:', result)
  1. strings ← [' Alice ', 'Bob ', ' Carol', 'Dave', ' Eve ']

    1strings = ['  Alice  ', 'Bob ', '  Carol', 'Dave', '  Eve  ']2result = []
    values this step[' Alice ', 'Bob ', ' Carol', 'Dave', ' Eve ']strings
  2. result ← []

    1strings = ['  Alice  ', 'Bob ', '  Carol', 'Dave', '  Eve  ']2result = []3for s in strings:
    values this step[]result
  3. s ← ' Alice '

    2result = []3for s in strings:4    result.append(s.strip())
    values this step' Alice 's
  4. result ← ['Alice']

    3for s in strings:4    result.append(s.strip())5print('RESULT:', result)
    values this step[] ['Alice']result
  5. s ← 'Bob '

    2result = []3for s in strings:4    result.append(s.strip())
    values this step' Alice ' 'Bob 's
  6. result ← ['Alice', 'Bob']

    3for s in strings:4    result.append(s.strip())5print('RESULT:', result)
    values this step['Alice'] ['Alice', 'Bob']result
  7. s ← ' Carol'

    2result = []3for s in strings:4    result.append(s.strip())
    values this step'Bob ' ' Carol's
  8. result ← ['Alice', 'Bob', 'Carol']

    3for s in strings:4    result.append(s.strip())5print('RESULT:', result)
    values this step['Alice', 'Bob'] ['Alice', 'Bob', 'Carol']result
  9. s ← 'Dave'

    2result = []3for s in strings:4    result.append(s.strip())
    values this step' Carol' 'Dave's
  10. result ← ['Alice', 'Bob', 'Carol', 'Dave']

    3for s in strings:4    result.append(s.strip())5print('RESULT:', result)
    values this step['Alice', 'Bob', 'Carol'] ['Alice', 'Bob', 'Carol', 'Dave']result
  11. s ← ' Eve '

    2result = []3for s in strings:4    result.append(s.strip())
    values this step'Dave' ' Eve 's
  12. result ← ['Alice', 'Bob', 'Carol', 'Dave', 'Eve']

    3for s in strings:4    result.append(s.strip())5print('RESULT:', result)
    values this step['Alice', 'Bob', 'Carol', 'Dave'] ['Alice', 'Bob', 'Carol', 'Dave', 'Eve']result
  13. for s in strings:

    2result = []3for s in strings:4    result.append(s.strip())
  14. stdout ← RESULT: ['Alice', 'Bob', 'Carol', 'Dave', 'Eve']

    4    result.append(s.strip())5print('RESULT:', result)
    values this stepRESULT: ['Alice', 'Bob', 'Carol', 'Dave', 'Eve']stdout

With pandas

df['x'].str.strip() returns a new Series with every string stripped. The dtype stays object (strings). The snapshot shows the cleaned values alongside the original dtype.

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

strings = ['  Alice  ', 'Bob ', '  Carol', 'Dave', '  Eve  ']
df = pd.DataFrame({'x': strings})
s = df['x'].str.strip()
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: ['Alice', 'Bob', 'Carol', 'Dave', 'Eve']
RESULT: ['Alice', 'Bob', 'Carol', 'Dave', 'Eve']

Implementation notes

  • str.strip() removes all leading and trailing whitespace characters (spaces, tabs, newlines). Use str.lstrip() to strip only the left side, or str.rstrip() for only the right.
  • Pass a character string to strip specific characters instead of whitespace: s.strip('.') removes leading/trailing dots.
  • Whitespace inside the string is unaffected — use str.replace or regex to collapse internal spaces.
  • Cross-reference: normalize-case (this chapter) — strip and lowercase are often applied together as the first cleaning step.