Pandas Series.str.strip()() function
Series.str.strip() is a function in Pandas used to remove whitespace from both ends of strings.
During data processing, we often need to clean user input or text data read from files, which may contain extra spaces, tabs, or newlines at both ends.strip() The function removes whitespace from both ends of strings, making the data cleaner.
Word meaning:strip means "to peel off or remove", here indicating removing whitespace from both ends of a string.
Basic syntax and parameters
str.strip() is a string accessor method of Series, so you need a Series containing strings first, and then call it through the .str accessor.
Syntax format
Series.str.strip(to_strip=None)
Parameter Description
| Parameters | Type | Required | Description | Default value |
|---|---|---|---|---|
| to_strip | str | Optional | Specifies the set of characters to remove. If not specified, whitespace (including spaces, tabs, newlines, etc.) is removed by default. | None (whitespace) |
Function description
- Return Value: Returns a new Series with whitespace removed from both ends.
- effect: Removes whitespace from both ends of each string element; whitespace in the middle remains unchanged.
- Note: Only removes characters at both ends of the string; characters in the middle are not affected.
Example
Let us thoroughly master str.strip() through a series of examples from simple to complex.
Example 1: Basic Usage - Removing Whitespace from Both Ends
Example
import
pandas
as
pd
Create a Series with whitespace characters
s
=
pd.
Series
(
[
' hello '
,
'tworldt'
,
'nrunoobn'
,
' python '
]
)
Use str.strip() to remove whitespace from both ends
result
=
s.
str
.
strip
(
)
(
"Original Series:"
)
(
repr
(
s
)
)
Use repr() to display the raw characters (including invisible ones)
(
"nResult after removing whitespace:"
)
(
result
)
Output result:
原始 Series:
0 ' hello '
1 'tworldt'
2 'nrunoobn'
3 ' python '
dtype: object
Result after removing whitespace:
0 hello
1 world
2 runoob
3 python
Code analysis:
pd.Series([' hello ', ...])Created a Series with whitespace characters.repr(s)is used to display the raw content of a string, including invisible characters such as spaces and tabs.s.str.strip()Removes whitespace from both ends of each string.- Whitespace in the middle of the string remains unchanged.
Example 2: Removing Specified Characters
can be used via to_strip The parameter specifies the characters to remove.
Example
import
pandas
as
pd
Create a Series with specific characters
s
=
pd.
Series
(
[
'xxxhello xxx'
,
'yyyworldyyy'
,
'zzzrunoobzzz'
]
)
Remove 'x' characters from both ends
result_x
=
s.
str
.
strip
(
'x'
)
Remove 'y' characters from both ends
result_y
=
s.
str
.
strip
(
'y'
)
Remove 'z' characters from both ends
result_z
=
s.
str
.
strip
(
'z'
)
(
"Original Series:"
)
(
s
)
(
"nResult after removing 'x':"
)
(
result_x
)
(
"nResult after removing 'y':"
)
(
result_y
)
(
"nResult after removing 'z':"
)
(
result_z
)
Output result:
原始 Series:
0 xxxhello xxx
1 yyyworldyyy
2 zzzrunoobzzz
dtype: object
Result after removing 'x':
0 hello xxx
1 yyyworldyyy
2 zzzrunoobzzz
Result after removing 'y':
0 xxxhello xxx
1 worldyyy
2 zzzrunoobzzz
Result after removing 'z':
0 xxxhello xxx
1 yyyworldyyy
2 runoobzzz
Code analysis:
strip('x')Only 'x' characters at both ends of the string are removed.- If the same characters appear in the middle of the string, they are not removed.
- Only characters appearing consecutively at both ends are removed.
Example 3: Handling Whitespace Issues in Real Data
When processing data from files or user input,strip() it is very useful.
Example
import
pandas
as
pd
Simulate username data collected from a form (possibly with extra spaces)
usernames
=
pd.
Series
(
[
' admin '
,
'user123'
,
' moderator '
,
'guest'
]
)
Clean up usernames
cleaned_usernames
=
usernames.
str
.
strip
(
)
(
"Original usernames:"
)
(
usernames
)
(
"nCleaned usernames:"
)
(
cleaned_usernames
)
Compare after removing whitespace
(
"nEqual after removing whitespace:"
)
(
cleaned_usernames
==
'admin'
)
Output result:
原始用户名:
0 admin
1 user123
2 moderator
3 guest
dtype: object
Cleaned username:
0 admin
1 user123
2 moderator
3 guest
Cleaned username:
0 True
1 False
2 False
3 False
Code analysis:
strip()Cleaned the leading and trailing spaces of the usernames.- This ensures that the user-entered
' admin 'matches correctly'admin'。 - This is an important step in data cleaning that avoids match failures caused by spaces.
Related functions
Pandas also provides other functions for removing whitespace:
str.lstrip()- Removes only leading (left-side) whitespace from the string.str.rstrip()- Removes only trailing (right-side) whitespace from the string.
Example
import
pandas
as
pd
s
=
pd.
Series
(
[
' hello world '
]
)
(
"Original string:"
,
repr
(
s
[
0
]
)
)
(
"strip():"
,
repr
(
s.
str
.
strip
(
)
[
0
]
)
)
Remove both ends
(
"lstrip():"
,
repr
(
s.
str
.
lstrip
(
)
[
0
]
)
)
Remove the left side
(
"rstrip():"
,
repr
(
s.
str
.
rstrip
(
)
[
0
]
)
)
Remove the right side
Output result:
原始字符串: ' hello world '
strip(): 'hello world'
lstrip(): 'hello world '
rstrip(): ' hello world'
Notes
str.strip()Removes whitespace by default, including spaces, tabs (t), newlines (n), carriage returns (r), etc.- This function only removes characters at both ends of the string; characters in the middle are not affected.
- If the Series contains NaN values,
strip()NaN is returned. - This function returns a new Series and does not modify the original data.
Common Pandas functions