str.strip()
Trim whitespace — or any set of characters — from both ends of the string.
Common call
" hi ".strip()
Returns
new str — original unchanged
Replaces
chars is a SET of characters, not a substring
Watch out
only the ends are trimmed — inner whitespace stays
str.strip(charschars — The set of characters to remove from both ends, in any order and combination. None (the default) strips whitespace.type: str | None · default: None=None)
→ str
Demo
Live evaluation
Try:
Inputs
stringstrthe source
charsstrempty = whitespace
Output
' hello '.strip()
'hello'
strip removes characters from both ends until it meets one not in the set. chars is treated as a set — "xy" removes x’s and y’s in any order. Leave the field empty for the whitespace default.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| chars | str | None | no (None) | The set of characters to remove from both ends, in any order and combination. None (the default) strips whitespace. |
Return value
str — A new string with leading and trailing characters removed. The middle is never touched.
Common patterns
Clean user input
The near-universal first step before validating a form field.
username = raw.strip()
Trim punctuation
Remove a known set of wrapper characters in one call.
"'quoted'".strip("'\"") # 'quoted'
Clean every line of a file
Drops the trailing newline and any stray spaces per line.
lines = [ln.strip() for ln in f]
Examples
1. Strip whitespace (default)
" hello ".strip()
Returns
'hello'2. Strip a set of characters
"xxhelloyy".strip("xy")
Returns
'hello'3. Inner whitespace survives
" a b ".strip()
Returns
'a b'4. Nothing to strip
"hello".strip()
Returns
'hello'Pitfalls
1. chars is a character set, not a substring
strip("ab") does not remove the sequence "ab" — it removes every a and b from the ends.
Surprising
"banana".strip("ban")
''
Remove a prefix instead
"banana".removeprefix("ban")
'ana'
2. The result must be assigned
Strings are immutable — strip returns a new string.
Wrong
s = " hi " s.strip() print(repr(s))
' hi '
Fix
s = s.strip() print(repr(s))
'hi'
3. Inner whitespace is not touched
strip only works at the ends; collapsing inner runs needs split/join.
Not enough
" a b ".strip()
'a b'
Collapse inner too
" ".join(" a b ".split())
'a b'
When to use
Use it
- Cleaning user input before validation
- Removing trailing newlines from file lines
- Trimming a known set of wrapper characters
Reach for something else
- Removing an exact prefix/suffix → str.removeprefix / str.removesuffix
- One side only → str.lstrip / str.rstrip
- Collapsing inner whitespace → " ".join(s.split())
Notes
Complexity
O(n) — scans from both ends
Return
new str — source untouched
CPython impl
Objects/unicodeobject.c :: do_strip
Memory
One new string sized to the kept slice
Thread-safe
Yes — str is immutable
FAQ
Use lstrip or rstrip — same semantics, one end.
" hi ".rstrip() # ' hi'
History
3.9
Related: str.removeprefix / str.removesuffix added for exact-substring trimming.
2.2
chars argument added — earlier versions stripped whitespace only.