Python strip() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

strip() removes unwanted characters from both ends of a string. With no argument it removes whitespace: spaces, tabs, and newlines. Characters in the middle are never touched, so " a b ".strip() is "a b".

You can pass a string of characters to remove instead. That argument is a set of characters, not a prefix or a suffix: strip("abc") removes any mix of a, b, and c from either end, for as long as the text keeps starting or ending with one of them. A string is never edited in place, so the method returns a new string.

text.strip() is a method of the built-in str type: call it on a string with dot notation. No import is needed, and it never changes the original string.

Syntax

Call it with no argument to trim whitespace, or pass the characters to remove.

python
clean = text.strip()
clean = text.strip(chars)
CallWhat it does
" hello ".strip()Returns 'hello'. Spaces on both sides are removed.
"xxhixx".strip("x")Returns 'hi'. Every x at either end is removed.
"www.site.com".strip("w.moc")Returns 'site'. The argument is a set of characters, so w, ., m, o, and c are all removed from the ends.

To trim only one side use lstrip() or rstrip(). To remove an exact prefix or suffix use removeprefix() or removesuffix().

Library declarations
python
str.strip([chars]) -> str
# returns: a new string; the original is unchanged
# chars: a string of characters to remove; default is whitespace

Parameters

ParameterDescription
charsOptional. A string whose characters are removed from both ends. The order does not matter. If it is omitted or None, whitespace is removed.

Return Value

Return ValueDescription
strA new string with the matching characters removed from the start and the end. The original string is not changed.

Python Examples

Example 1 — Remove Spaces Around Text

Strip spaces, a tab, and a newline, and keep the spaces inside the text.

python
text = "  \t Learn Python \n"
clean = text.strip()

print(repr(text))
print(repr(clean))
print(len(text), len(clean))

How It Works

  1. All kinds of whitespace. strip() removes the spaces, the tab, and the newline at both ends.
  2. The middle stays. The space between Learn and Python is not at an end, so it is kept.
  3. A shorter copy. The original has 18 characters and the clean text has 12. The original is unchanged.

Example 2 — Normalize a Typed Email

Strip the spaces a user typed and lower the text before using it.

python
typed = "  [email protected] \n"
email = typed.strip().lower()

print(email)
print(email == "[email protected]")
print(typed == "[email protected]")

How It Works

  1. Chain the methods. strip() returns a string, so lower() can be called on the result straight away.
  2. The clean value matches. After trimming and lowering, the email equals the expected text.
  3. The raw value does not. The typed text still has spaces, a newline, and capitals, so the comparison is False.

Example 3 — Strip a Set of Characters

Remove decorations from both ends, and see what happens when the characters also appear inside a word.

python
print("--==Title==--".strip("-="))
print("cabbage".strip("abc"))
print("banana".strip("an"))

How It Works

  1. A set of two characters. The argument "-=" removes any hyphens and equals signs from both ends, leaving Title.
  2. Not a prefix. "cabbage" loses c, a, b, b, and a from the left, because each one is in the set, and stops at g. Nothing is removed from the right, since e is not in the set.
  3. It keeps going. From the right, "banana" loses a, n, a, n, and a, and the left side stops at b, which is not in the set.

Frequently Asked Questions

It returns a copy of the string with whitespace removed from both ends. If you pass characters, it removes any of those characters from both ends instead.
No. It only works on the two ends. To remove spaces inside the text use replace(" ", "") or split() and join().
strip() trims both ends, lstrip() trims only the left end, and rstrip() trims only the right end.
The argument is a set of characters. strip("abc") removes every a, b, or c from the ends until another character appears. Use removeprefix() or removesuffix() for exact text.
No. Strings are immutable, so strip() returns a new string. Assign the result, for example text = text.strip().

Next: swapcase()

Return a copy with upper case letters made lower and lower case made upper.

swapcase() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

16 people found this page helpful