Python String strip() Method

Short answer
text.strip(chars=None) returns a new string with leading and trailing whitespace removed, or with any characters listed in chars removed from both ends.

Text from users, files and web forms is often padded with spaces or ends with a newline. strip() cleans both ends in one call, which makes it one of the first methods to apply to input before comparing or converting it. It never touches whitespace inside the string.

If you only need one side, use lstrip() for the left end or rstrip() for the right end.

Quick facts
Syntaxtext.strip(chars=None)
Parameterschars (optional): a string of characters to remove; default is all whitespace
ReturnsA new str; the original is unchanged
Time complexityO(n) in the length of the string

How strip() removes whitespace from both ends

By default, whitespace means spaces, tabs, newlines and other Unicode whitespace characters. Python removes them from the start until it meets a different character, then from the end in the same way.

Python
raw = "  \t Asha Rao \n"
print(repr(raw))
print(repr(raw.strip()))

The chars argument is a set of characters, not a word

This is the most common surprise. chars is treated as a collection of individual characters, not as a prefix or suffix. Python keeps removing characters from each end as long as they appear anywhere in chars. Order does not matter.

Python
print("xxhello worldxy".strip("xy"))
print("www.web.com".strip("w."))

The second line shows the trap: every leading w and . is removed, including the w that belongs to web, so you get eb.com. To remove a whole prefix or suffix, use removeprefix() and removesuffix() (Python 3.9+).

strip() vs lstrip() vs rstrip()

MethodRemoves fromResult for " hi "
strip()Both ends'hi'
lstrip()Left (start) only'hi '
rstrip()Right (end) only' hi'
replace(" ", "")Everywhere, including the middle'hi'
" ".join(s.split())Both ends and collapses inner runs'hi'

Cleaning user input before using it

Combine strip() with lower() to compare typed answers reliably, and call it before converting with int() or float(). Note that int(" 42 ") already ignores outer spaces, but custom comparisons such as answer == "yes" do not.

Examples you can run

Press Run, then edit the code and run it again.

Remove spaces and a newline from input

strip() removes every kind of whitespace at the ends.

Remove spaces and a newline from input
line = "   Asha   \n"
name = line.strip()
print(f"[{name}]")
Expected output[Asha]

Strip chosen characters

Pass the characters to remove from both ends.

Strip chosen characters
price = "$$19.99$$"
print(price.strip("$"))
banner = "***Sale***"
print(banner.strip("*"))
Expected output19.99
Sale

Normalise an answer before comparing

Strip and lower-case first, then compare.

Normalise an answer before comparing
answer = "  YES "
print(answer == "yes")
print(answer.strip().lower() == "yes")
Expected outputFalse
True

Common mistakes

Expecting the original string to change

✗ Wrong

name = "  Asha  "
name.strip()
print(f"[{name}]")
[ Asha ] (nothing changed)

✓ Right

name = "  Asha  "
name = name.strip()
print(f"[{name}]")

Strings are immutable; assign the result back.

Thinking chars removes a whole word

✗ Wrong

print("python.txt".strip(".txt"))
python (works by luck) but "text.txt".strip(".txt") returns an empty string

✓ Right

print("text.txt".removesuffix(".txt"))

Use removesuffix() to remove an exact ending.

Expecting inner spaces to disappear

✗ Wrong

print("  a   b  ".strip())
a b (inner spaces remain)

✓ Right

print(" ".join("  a   b  ".split()))

Use split() and join() to collapse inner whitespace.

Python String strip() Method: questions people ask

What does strip() do in Python?

It removes whitespace, or the characters you specify, from the beginning and end of a string and returns the result as a new string.

How do I remove all spaces from a string in Python?

strip() only handles the ends. Use text.replace(" ", "") to remove every space, or "".join(text.split()) to remove all whitespace.

Does strip() remove newline characters?

Yes. Newlines, tabs and spaces at either end are all whitespace and are removed by default.

Keep learning

Step-by-step lessons:
Python StringsPython String Methods

Related:
Python String lstrip() MethodPython String rstrip() MethodHow to Remove Whitespace from a String in PythonPython String replace() MethodPython String split() Method

More in string methods:
Python String split() MethodPython String join() MethodPython String lstrip() MethodPython String rstrip() MethodPython String replace() MethodPython String upper() MethodPython String lower() MethodPython String title() Method See all →