Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Pandas Series.str.strip() function

Pandas Series.str.strip()() function

Pandas Common Functions Common Pandas functions


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


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

(

)

print

(

"Original Series:"

)

print

(

repr

(

s

)

)

 

Use repr() to display the raw characters (including invisible ones)

print

(

"nResult after removing whitespace:"

)

print

(

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:

  1. pd.Series([' hello ', ...]) Created a Series with whitespace characters.
  2. repr(s) is used to display the raw content of a string, including invisible characters such as spaces and tabs.
  3. s.str.strip() Removes whitespace from both ends of each string.
  4. 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'

)

print

(

"Original Series:"

)

print

(

s

)

print

(

"nResult after removing 'x':"

)

print

(

result_x

)

print

(

"nResult after removing 'y':"

)

print

(

result_y

)

print

(

"nResult after removing 'z':"

)

print

(

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:

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

(

)

print

(

"Original usernames:"

)

print

(

usernames

)

print

(

"nCleaned usernames:"

)

print

(

cleaned_usernames

)

Compare after removing whitespace

print

(

"nEqual after removing whitespace:"

)

print

(

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:


Related functions

Pandas also provides other functions for removing whitespace:

Example

import

pandas

as

pd

s

=

pd.

Series

(

[

'  hello world  '

]

)

print

(

"Original string:"

,

repr

(

s

[

0

]

)

)

print

(

"strip():"

,

repr

(

s.

str

.

strip

(

)

[

0

]

)

)

     

Remove both ends

print

(

"lstrip():"

,

repr

(

s.

str

.

lstrip

(

)

[

0

]

)

)

   

Remove the left side

print

(

"rstrip():"

,

repr

(

s.

str

.

rstrip

(

)

[

0

]

)

)

   

Remove the right side

Output result:

原始字符串: '  hello world  '

strip(): 'hello world'

lstrip(): 'hello world  '

rstrip(): '  hello world'


Notes


Pandas Common Functions Common Pandas functions