0
0
Pandasdata~10 mins

str.strip() for whitespace in Pandas - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - str.strip() for whitespace
Start with Series of strings
Apply str.strip()
Remove leading/trailing whitespace
Return cleaned Series
This flow shows how applying str.strip() on a pandas Series removes spaces at the start and end of each string, returning a cleaned Series.
Execution Sample
Pandas
import pandas as pd
s = pd.Series(['  apple ', ' banana', 'cherry  '])
clean_s = s.str.strip()
print(clean_s)
This code creates a Series with strings having spaces, then removes those spaces using str.strip(), and prints the cleaned Series.
Execution Table
StepInput StringActionOutput String
1' apple 'Remove leading/trailing spaces'apple'
2' banana'Remove leading/trailing spaces'banana'
3'cherry 'Remove leading/trailing spaces'cherry'
4All strings processedReturn cleaned Series['apple', 'banana', 'cherry']
💡 All strings processed, whitespace removed from start and end
Variable Tracker
VariableStartAfter str.strip()Final
s[' apple ', ' banana', 'cherry ']['apple', 'banana', 'cherry']['apple', 'banana', 'cherry']
clean_sN/A['apple', 'banana', 'cherry']['apple', 'banana', 'cherry']
Key Moments - 2 Insights
Why does str.strip() only remove spaces at the start and end, not inside the string?
str.strip() is designed to remove whitespace only from the beginning and end of each string, not from the middle. This is shown in the execution_table where only leading/trailing spaces are removed.
What happens if a string has no spaces at the start or end?
If there are no spaces at the start or end, str.strip() leaves the string unchanged. This is implied in the execution_table where each string is processed but only spaces at edges are removed.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output string for the input ' banana' after str.strip()?
A' banana'
B'banana '
C'banana'
D' ban ana '
💡 Hint
Check row 2 in the execution_table where ' banana' is processed.
At which step does the Series return the fully cleaned list of strings?
AStep 2
BStep 4
CStep 3
DStep 1
💡 Hint
Look at the exit note and the last row in the execution_table.
If a string was ' kiwi fruit ', what would str.strip() output be?
A'kiwi fruit'
B'kiwifruit'
C' kiwi fruit '
D' kiwi fruit'
💡 Hint
str.strip() removes spaces only at start and end, not inside the string.
Concept Snapshot
pandas Series.str.strip()
Removes spaces at start and end of each string
Does NOT remove spaces inside strings
Returns a new Series with cleaned strings
Useful for cleaning messy text data
Full Transcript
We start with a pandas Series containing strings with spaces at the start or end. Applying str.strip() removes these spaces from each string. The cleaned strings are returned as a new Series. This method only removes whitespace at the edges, not inside the strings. For example, ' apple ' becomes 'apple'. This helps clean text data before analysis.