Python String strip() Method
The Python strip() method removes characters from the beginning and end of a string.
Beginners usually use it to remove extra whitespace, such as spaces, tabs, and newline characters. This is especially useful when cleaning user input or processing text from a file.
Quick answer
text = " hello "
clean_text = text.strip()
print(clean_text)
Output:
hello
Use strip() to remove whitespace from both ends of a string. It does not change the original string.
What strip() does
strip() is a string method that returns a new string.
Key points:
strip()returns a new string- It removes characters from the beginning and end only
- By default, it removes whitespace
- It does not remove characters from the middle of the string
- Strings are immutable, so the original string stays the same
Example:
text = " hello world "
print(text.strip())
print(text)
Output:
hello world
hello world
The cleaned version is returned by strip(), but the original text value is unchanged.
If you are new to strings, see Python strings explained: basics and examples.
Basic syntax
There are two common forms:
string.strip()string.strip(chars)
string.strip()
This removes whitespace from both ends:
name = " Alice "
print(name.strip())
Output:
Alice
string.strip(chars)
This removes any matching characters from both ends:
text = "!!hello??"
print(text.strip("!?"))
Output:
hello
Important:
charsmeans a set of characters to remove from both ends- The order of characters in
charsdoes not matter
Remove whitespace with strip()
This is the most common use of strip().
It is useful for:
- Cleaning
input()values - Cleaning lines read from a file
- Removing spaces before checking or comparing text
- Removing tabs and newline characters at both ends
Example with user input:
username = " sam "
clean_username = username.strip()
print(clean_username)
Output:
sam
This matters when comparing strings:
answer = " yes "
if answer.strip() == "yes":
print("Matched")
Output:
Matched
If you want a broader guide, see how to remove whitespace from a string in Python.
Example with file-style text:
line = "Python basics\n"
print(repr(line))
print(repr(line.strip()))
Output:
'Python basics\n'
'Python basics'
Using repr() helps you see hidden characters like \n.
If you are working with files, see how to read a file line by line in Python.
Remove specific characters
You can pass characters as an argument, such as strip("!?").
Python will:
- Remove any matching character from both ends
- Keep removing until it finds a character that is not in the set
- Leave the middle of the string unchanged
Example:
text = "??hello!!"
print(text.strip("!?"))
Output:
hello
Another example:
text = "abcHelloCba"
print(text.strip("abc"))
Output:
HelloC
This can surprise beginners.
strip("abc") does not mean “remove the exact text abc once”. It means “remove any a, b, or c characters from both ends”.
It also does not remove characters from the middle.
If you need to replace text anywhere in a string, use replace() instead.
strip() vs lstrip() vs rstrip()
These methods are similar, but they remove characters from different sides.
strip()removes from both endslstrip()removes from the left side onlyrstrip()removes from the right side only
Example:
text = " hello "
print(repr(text.strip()))
print(repr(text.lstrip()))
print(repr(text.rstrip()))
Output:
'hello'
'hello '
' hello'
Choose the method based on which side needs cleaning.
Important behavior to understand
These points are worth remembering:
strip("abc")does not remove the exact text"abc"once- It removes any of the characters
a,b, orcfrom both ends - Middle characters are untouched
- Save the result to a variable if you want to use the cleaned string
Example:
filename = "txt_report.txt"
print(filename.strip(".txt"))
Output:
_report
This happens because Python removes any ., t, or x characters from both ends.
So strip(".txt") is not a safe way to remove a file extension.
If you need to change exact text in a string, replace() is often a better fit for simple cases.
When to use strip()
Use strip() when you need to clean text at the edges of a string.
Common cases:
- Cleaning
input()values - Cleaning lines read from a file
- Removing accidental spaces before validation
- Preparing text before comparisons
Example:
email = " user@example.com "
if email.strip():
print("Email was entered")
Output:
Email was entered
You may also combine strip() with other string methods. For example, after removing extra spaces, you might convert text to lowercase with lower().
Common mistakes
Beginners often run into these problems:
- Expecting
strip()to change the original string - Expecting
strip()to remove text from the middle - Using
strip(".txt")and expecting only the exact file extension to be removed - Forgetting that
strip(chars)removes any matching characters, not a whole substring
Here is a quick debugging example:
text = " hello\n"
print(text)
print(repr(text))
print(text.strip())
print(repr(text.strip()))
Output:
hello
' hello\n'
hello
'hello'
Useful debugging commands:
print(text)
print(repr(text))
print(text.strip())
print(repr(text.strip()))
help(str.strip)
repr() is very helpful because it shows hidden whitespace clearly.
FAQ
Does strip() remove spaces in the middle of a string?
No. strip() only removes characters from the start and end of the string.
Does strip() change the original string?
No. It returns a new string because Python strings are immutable.
What does strip() remove by default?
It removes whitespace such as spaces, tabs, and newline characters from both ends.
How is strip() different from replace()?
strip() removes characters only from the ends. replace() can change matching text anywhere in the string.
Can I remove only the left or right side?
Yes. Use lstrip() for the left side and rstrip() for the right side.