Python Reference
str.strip()
Remove leading and trailing whitespace or custom characters.
Last updated: October 2026
Definition: str.strip(chars=None) removes leading and trailing whitespace (or specified characters) and returns a new string. The original string is unchanged.
🎬 Live Example
Five common strip patterns. Click any button to see the output.
💡 strip returns a new string — strings are immutable in Python.
Basic Syntax
str.strip(chars=None)
str.lstrip(chars=None)
str.rstrip(chars=None)
# chars — optional set of characters to remove
# Default: whitespace (space, tab, newline)
# Returns: new str
# Does NOT modify the original
🎯 Parameters
| Parameter | Type | Description |
|---|---|---|
chars | str or None | Characters to remove. None = whitespace. |
Strip Whitespace (Default)
" hello ".strip() # 'hello'
"\nhello\t".strip() # 'hello'
" hello world ".strip() # 'hello world'
lstrip and rstrip
" hello ".lstrip() # 'hello ' (left only)
" hello ".rstrip() # ' hello' (right only)
# lstrip is left, rstrip is right
Custom Characters
"xxhelloxx".strip("x") # 'hello'
"---hello---".strip("-") # 'hello'
"www.example.com".strip("w") # '.example.com' (careful!)
# chars is a set of chars, not a substring
"abc123".strip("abc123") # '' (removes everything)
Important: chars is treated as a set of individual characters to strip, not a substring.
Middle Characters Preserved
" hello world ".strip()
# 'hello world' — middle space kept
"xxhelloxxworldxx".strip("x")
# 'helloxxworld' — only ends stripped
Real-World: Clean User Input
user_input = " alice@example.com \n"
email = user_input.strip()
# 'alice@example.com'
# Clean lines from a file
with open("data.txt") as f:
lines = [line.strip() for line in f]
strip is the go-to method for sanitizing text before processing.
🎯 strip vs lstrip vs rstrip vs replace
| Method | Removes from | Use when |
|---|---|---|
strip | Both ends | General cleanup |
lstrip | Left only | Remove leading chars |
rstrip | Right only | Remove trailing newlines |
replace | Anywhere | Remove from middle too |
⚠️ Common Mistakes
- Thinking strip modifies the string. Strings are immutable. Assign the result.
- Expecting strip to remove substrings. It removes individual characters.
- Using strip to clean the middle. Use
replacefor middle characters. - Stripping without assigning.
s.strip()alone does nothing. - Assuming rstrip removes only newlines. It removes whitespace (including spaces) from the right.
❓ Frequently Asked Questions
What does strip() do in Python?
str.strip() removes leading and trailing whitespace (or specified characters) from a string and returns a new string. The original string is unchanged.
What is the difference between strip, lstrip, and rstrip?
strip removes from both ends. lstrip removes only from the left. rstrip removes only from the right. All three accept an optional chars argument.
Does strip() remove characters from the middle?
No. strip only removes leading and trailing characters. Characters in the middle are preserved.
How do I strip custom characters?
Pass the characters as a string: 'xxhelloxx'.strip('x') returns 'hello'. The chars argument is a set of characters to remove, not a substring.
Does strip modify the original string?
No. Strings are immutable in Python. strip returns a new string; the original is unchanged.