Master Code
On The Go

Learn. Practice. Build.

Home › Code Snippets › Python › Reference › strip()

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.

Input:

                
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
charsstr or NoneCharacters to remove. None = whitespace.
1

Strip Whitespace (Default)

"  hello  ".strip()      # 'hello'
"\nhello\t".strip()       # 'hello'
"  hello world  ".strip() # 'hello world'
2

lstrip and rstrip

"  hello  ".lstrip()  # 'hello  ' (left only)
"  hello  ".rstrip()  # '  hello' (right only)

# lstrip is left, rstrip is right
3

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.

4

Middle Characters Preserved

"  hello world  ".strip()
# 'hello world' — middle space kept

"xxhelloxxworldxx".strip("x")
# 'helloxxworld' — only ends stripped
5

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
stripBoth endsGeneral cleanup
lstripLeft onlyRemove leading chars
rstripRight onlyRemove trailing newlines
replaceAnywhereRemove from middle too

⚠️ Common Mistakes

❓ 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.

🎯 What's Next?

← All Python Reference