String Cleaning
Strip Whitespace
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)
strings ← [' Alice ', 'Bob ', ' Carol', 'Dave', ' Eve ']
1strings = [' Alice ', 'Bob ', ' Carol', 'Dave', ' Eve ']2result = []values this step[' Alice ', 'Bob ', ' Carol', 'Dave', ' Eve ']stringsresult ← []
1strings = [' Alice ', 'Bob ', ' Carol', 'Dave', ' Eve ']2result = []3for s in strings:values this step[]results ← ' Alice '
2result = []3for s in strings:4 result.append(s.strip())values this step' Alice 'sresult ← ['Alice']
3for s in strings:4 result.append(s.strip())5print('RESULT:', result)values this step[] → ['Alice']results ← 'Bob '
2result = []3for s in strings:4 result.append(s.strip())values this step' Alice ' → 'Bob 'sresult ← ['Alice', 'Bob']
3for s in strings:4 result.append(s.strip())5print('RESULT:', result)values this step['Alice'] → ['Alice', 'Bob']results ← ' Carol'
2result = []3for s in strings:4 result.append(s.strip())values this step'Bob ' → ' Carol'sresult ← ['Alice', 'Bob', 'Carol']
3for s in strings:4 result.append(s.strip())5print('RESULT:', result)values this step['Alice', 'Bob'] → ['Alice', 'Bob', 'Carol']results ← 'Dave'
2result = []3for s in strings:4 result.append(s.strip())values this step' Carol' → 'Dave'sresult ← ['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']results ← ' Eve '
2result = []3for s in strings:4 result.append(s.strip())values this step'Dave' → ' Eve 'sresult ← ['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']resultfor s in strings:
2result = []3for s in strings:4 result.append(s.strip())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). Usestr.lstrip()to strip only the left side, orstr.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.replaceor regex to collapse internal spaces. - Cross-reference:
normalize-case(this chapter) — strip and lowercase are often applied together as the first cleaning step.