str.strip()
Removes spaces, tabs and new lines from the start and end of a string, or any set of characters you list.
str.strip(chars=None)strip() gives you a copy of the string without the whitespace at both ends: spaces, tabs (\t) and new lines (\n). Spaces in the middle stay where they are.
It is the standard clean-up for anything a person typed or a file gave you: a line read from a file still ends in \n, and users often add a space by accident before or after their answer.
The big surprise: strip("abc") does not remove the text "abc". It removes any of the characters a, b and c, in any order, from both ends, until it meets a character not in that list. To remove an exact piece of text from the start or end, use str.removeprefix() or str.removesuffix().
Parameters
| Parameter | What it is |
|---|---|
charsoptional, default None | The characters to remove, given as one string. Order does not matter: each character is removed wherever it appears at an end. With None, all whitespace is removed. |
Return value
A new string without those characters at either end. The original string does not change.
Examples
1Remove spaces and the new line around text
name = " Ana Silva \n"
print(f"[{name}]")
print(f"[{name.strip()}]")[ Ana Silva ] [Ana Silva]
The square brackets show where the string really starts and ends. The space between the two names stays.
2Compare answers safely
A stray space makes two answers look the same to a person but different to Python.
answer = " yes \n"
print(answer == "yes")
print(answer.strip() == "yes")False True
3Remove other characters
price = "$$19.99$"
print(price.strip("$"))
title = "*** SALE ***"
print(title.strip("* "))19.99 SALE
"* " means: remove stars and spaces, in any mix.
4Clean every item of a list
tags = [" python", "data ", " web "]
clean = [t.strip() for t in tags]
print(clean)['python', 'data', 'web']
5Read numbers from a file, one per line
total = 0
with open("scores.txt") as f:
for line in f:
total += int(line.strip())
print("Total:", total)Total: 60
Common mistakes
Expecting strip() to remove a word
chars is a set of single characters, not a piece of text. Here strip(".txt") keeps removing t, x and dots from both ends until only e is left. Use removesuffix() for an exact ending.
filename = "text.txt"
print(filename.strip(".txt"))
print(filename.removesuffix(".txt"))e text
Forgetting to store the result
Strings never change in place. name.strip() on its own line does nothing useful: write name = name.strip().
name = " Omar "
name.strip()
print(f"[{name}]")
name = name.strip()
print(f"[{name}]")[ Omar ] [Omar]
Questions people ask
How do I remove only the spaces at the start, or only at the end?
Use str.lstrip() for the left side and str.rstrip() for the right side.
How do I remove all spaces from a string, also in the middle?
strip() only touches the ends. Use text.replace(" ", ""), or "".join(text.split()) to remove every kind of whitespace.
How do I remove the new line at the end of a line from a file?
line.rstrip("\n") removes only the new line; line.strip() also removes spaces at both ends.
Why did strip() remove more than I wanted?
Because each character in chars is removed separately. "banana".strip("ab") gives "nan". For an exact prefix or suffix, use removeprefix() or removesuffix().