In this Python tutorial, you will learn how to remove whitespace or selected characters from the beginning and end of a string using the str.strip() method.
Python strip() method for trimming strings
The strip() method returns a new string with characters removed from both ends. When no argument is supplied, it removes leading and trailing whitespace, including spaces, tabs, and newline characters.
Python strings are immutable, so strip() does not change the original string. Store the returned value in a variable when you need to use the trimmed result.
Syntax of Python string strip()
The syntax of strip() function is
string.strip(characters)
The optional characters argument is a string containing the individual characters that may be removed from the left and right edges.
- If
charactersis omitted, leading and trailing whitespace is removed. - If
charactersis supplied, Python repeatedly removes any matching character from either end. - The argument is treated as a set of characters, not as an exact prefix or suffix.
- Characters in the middle of the string are not removed.
Important: strip() removes matching characters only from the edges. It does not remove spaces, tabs, newlines, or selected characters that occur between other characters.
Python strip() examples
1. Trim spaces around a Python string
In the following example, the input string contains two spaces at the beginning and three spaces at the end.
Python Program
str = ' Hello TutorialKart '
#strip any white characters
strippedStr = str.strip()
print("Original String\n---------------")
print(str)
print("\nStripped String\n---------------")
print(strippedStr)
After strip() is called, the spaces at both ends are removed. The space between Hello and TutorialKart remains unchanged.
Output
Original String
---------------
Hello TutorialKart
Stripped String
---------------
Hello TutorialKart
2. Strip specified characters from both ends
Pass a string of removable characters to strip() when you need to trim symbols or other known characters from the edges.
Python Program
str = '---Hello TutorialKart@-@'
characters = '-@'
#strip the string
strippedStr = str.strip(characters)
print("Original String\n---------------")
print(str)
print("\nStripped String\n---------------")
print(strippedStr)
Python removes each leading or trailing - or @ character until it reaches a character that is not included in characters.
Output
Original String
---------------
---Hello TutorialKart@-@
Stripped String
---------------
Hello TutorialKart
3. Trim tabs and newlines from string edges
Calling strip() without an argument removes surrounding whitespace characters, not only ordinary spaces.
Python Program
str = ' \n\tHello TutorialKart '
#strip any white space characters
strippedStr = str.strip()
print("Original String\n---------------")
print(str)
print("\nStripped String\n---------------")
print(strippedStr)
The leading spaces, newline, tab, and trailing space are removed from the returned string.
Output
Original String
---------------
Hello TutorialKart
Stripped String
---------------
Hello TutorialKart
4. Keep whitespace inside the string
This example includes newline and tab characters both at the edge and inside the string. Only the edge whitespace is removed.
Python Program
str = ' \n\tHello\n\tTutorialKart '
#strip any white space characters
strippedStr = str.strip()
print("Original String\n---------------")
print(str)
print("\nStripped String\n---------------")
print(strippedStr)
The newline and tab before Hello and the trailing space are stripped. The newline and tab between Hello and TutorialKart remain part of the result.
Output
Original String
---------------
Hello
TutorialKart
Stripped String
---------------
Hello
TutorialKart
5. Understand why strip() does not remove an exact substring
The characters argument does not represent one complete prefix or suffix. Each character is considered separately.
text = "xyxPythonxy"
result = text.strip("xy")
print(result)
Python removes every leading or trailing x or y, so the result is:
Python
To remove one exact prefix or suffix, use removeprefix() or removesuffix() instead of strip().
strip(), lstrip(), and rstrip() differences
Python provides three related methods for trimming strings:
| Method | Characters removed |
|---|---|
strip() | From both the beginning and end |
lstrip() | From the beginning only |
rstrip() | From the end only |
text = " Python "
print(text.lstrip())
print(text.rstrip())
print(text.strip())
Use the method that matches the side from which characters should be removed.
Common Python strip() mistakes
- Expecting the original string to change: assign the return value because strings are immutable.
- Expecting internal spaces to be removed:
strip()works only at the beginning and end. - Passing a substring: the argument is a set of removable characters, not a complete word or token.
- Using strip() for one side only: choose
lstrip()orrstrip()when appropriate.
Python strip() frequently asked questions
Does Python strip() remove all spaces?
No. It removes whitespace only from the beginning and end of the string. Spaces between words remain unchanged.
Does strip() modify the original string?
No. It returns a new string because Python strings are immutable.
Can strip() remove tabs and newline characters?
Yes. With no argument, strip() removes leading and trailing whitespace such as spaces, tabs, newlines, carriage returns, form feeds, and vertical tabs.
How do I remove characters from only one end?
Use lstrip() for the left edge or rstrip() for the right edge.
Summary of trimming strings with strip()
In this Python Tutorial, you learned how strip() removes whitespace or selected characters from both ends of a string. You also learned that it leaves internal characters unchanged, returns a new string, and treats its optional argument as a set of characters rather than an exact substring.
TutorialKart.com