YouTip LogoYouTip

Pandas Series Str Strip

Pandas Series.str.strip() Function |\n\n

Image 1: Pandas Common functions Pandas General Functions

\n\n
\n\n

Series.str.strip() is a function in Pandas used to remove whitespace characters from both ends of strings.

\n\n

In data processing, we often need to clean text data that comes from user input or files, which may contain extra spaces, tabs, or newline characters at both ends. The strip() function can remove these leading and trailing whitespace characters, making the data cleaner and more organized.

\n\n

Word Meaning: strip means "to peel off" or "remove" in English, here it refers to removing whitespace characters at both ends of a string.

\n\n
\n\n

Basic Syntax and Parameters

\n\n

str.strip() is a method of the Series string accessor, so you first need a Series containing strings, then call it through the .str accessor.

\n\n

Syntax Format

\n
Series.str.strip(to_strip=None)
\n\n

Parameter Description

\n\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
ParameterTypeRequiredDescriptionDefault Value
to_stripstrOptionalSpecifies the set of characters to be removed. If not specified, it removes whitespace characters by default (including spaces, tabs, newlines, etc.).None (whitespace characters)
\n\n

Function Description

\n\n
    \n
  • Return Value: Returns a new Series with leading and trailing whitespace characters removed.
  • \n
  • Effect: Removes whitespace characters at both ends of each string element; whitespace in the middle remains unchanged.
  • \n
  • Note: Only removes characters at the beginning and end of strings; characters in the middle are unaffected.
  • \n
\n\n
\n\n

Examples

\n\n

Let's go through a series of examples from simple to complex to fully master the usage of str.strip().

\n\n

Example 1: Basic Usage - Remove Whitespace at Both Ends

\n\n

Code Example

\n
import pandas as pd\n\n# Create a Series with whitespace characters\n\ns = pd.Series([' hello ','tworldt','ntutorialn',' python '])\n\n# Use str.strip() to remove whitespace at both ends\n\nresult = s.str.strip()\n\nprint("Original Series:")\nprint(repr(s)) # Use repr() to show original characters (including invisible ones)\n\nprint("nResult after removing whitespace:")\nprint(result)\n
\n\n

Output Result:

\n
Original Series:\n0 ' hello '\n1 'tworldt'\n2 'ntutorialn'\n3 ' python '\ndtype: object\n\nResult after removing whitespace:\n0 hello\n1 world\n2 tutorial\n3 python\n
\n\n

Code Explanation:

\n\n
    \n
  1. pd.Series([' hello ', ...]) creates a Series with whitespace characters.
  2. \n
  3. repr(s) displays the raw content of strings, including spaces, tabs, etc.
  4. \n
  5. s.str.strip() removes whitespace at both ends of each string.
  6. \n
  7. Whitespace in the middle of strings remains unchanged.
  8. \n
\n\n

Example 2: Remove Specified Characters

\n\n

You can specify characters to remove using the to_strip parameter.

\n\n

Code Example

\n
import pandas as pd\n\n# Create a Series with specific characters\n\ns = pd.Series(['xxxhello xxx','yyyworldyyy','zzztutorialzzz'])\n\n# Remove 'x' characters at both ends\n\nresult_x = s.str.strip('x')\n\n# Remove 'y' characters at both ends\n\nresult_y = s.str.strip('y')\n\n# Remove 'z' characters at both ends\n\nresult_z = s.str.strip('z')\n\nprint("Original Series:")\nprint(s)\n\nprint("nResult after removing 'x':")\nprint(result_x)\n\nprint("nResult after removing 'y':")\nprint(result_y)\n\nprint("nResult after removing 'z':")\nprint(result_z)\n
\n\n

Output Result:

\n
Original Series:\n0 xxxhello xxx\n1 yyyworldyyy\n2 zzztutorialzzz\ndtype: object\n\nResult after removing 'x':\n0 hello xxx\n1 yyyworldyyy\n2 zzztutorialzzz\n\nResult after removing 'y':\n0 xxxhello xxx\n1 worldyyy\n2 zzztutorialzzz\n\nResult after removing 'z':\n0 xxxhello xxx\n1 yyyworldyyy\n2 tutorialzzz\n
\n\n

Code Explanation:

\n\n
    \n
  • strip('x') only removes 'x' characters at both ends of the string.
  • \n
  • If there are identical characters in the middle of the string, they won't be removed.
  • \n
  • Only consecutive characters at both ends will be removed.
  • \n
\n\n

Example 3: Handling Spaces in Real Data

\n\n

When dealing with data from files or user inputs, strip() is very useful.

\n\n

Code Example

\n
import pandas as pd\n\n# Simulate username data collected from forms (may contain extra spaces)\n\nusernames = pd.Series([' admin ','user123',' moderator ','guest'])\n\n# Clean usernames\n\ncleaned_usernames = usernames.str.strip()\n\nprint("Original usernames:")\nprint(usernames)\n\nprint("nCleaned usernames:")\nprint(cleaned_usernames)\n\n# Compare after stripping whitespace\n\nprint("nAre cleaned usernames equal to 'admin'?")\nprint(cleaned_usernames =='admin')\n
\n\n

Output Result:

\n
Original usernames:\n0 admin\n1 user123\n2 moderator\n3 guest\ndtype: object\n\nCleaned usernames:\n0 admin\n1 user123\n2 moderator\n3 guest\n\nAre cleaned usernames equal to 'admin'?\n0 True\n1 False\n2 False\n3 False\n
\n\n

Code Explanation:

\n\n
    \n
  • strip() removes spaces before and after usernames.
  • \n
  • This ensures that user input like ' admin ' correctly matches 'admin'.
  • \n
  • This is an important step in data cleaning to prevent matching failures due to extra spaces.
  • \n
\n\n
\n\n

Related Functions

\n\n

Pandas also provides other functions for removing whitespace:

\n\n
    \n
  • str.lstrip() - Removes whitespace characters only from the left (beginning) of strings.
  • \n
  • str.rstrip() - Removes whitespace characters only from the right (end) of strings.
  • \n
\n\n

Code Example

\n
import pandas as pd\n\ns = pd.Series([' hello world '])\n\nprint("Original string:",repr(s))\n\nprint("strip():",repr(s.str.strip())) # Remove both ends\n\nprint("lstrip():",repr(s.str.lstrip())) # Remove left side\n\nprint("rstrip():",repr(s.str.rstrip())) # Remove right side\n
\n\n

Output Result:

\n
Original string: ' hello world '\nstrip(): 'hello world'\nlstrip(): 'hello world '\nrstrip(): ' hello world'\n
\n\n
\n\n

Notes

\n\n
    \n
  • str.strip() removes whitespace characters by default, including spaces, tabs (t), newlines (n), carriage returns (r), etc.
  • \n
  • This function only removes characters at the start and end of strings; characters in the middle remain untouched.
  • \n
  • If the Series contains NaN values, strip() will return NaN.
  • \n
  • The function returns a new Series without modifying the original data.
  • \n
\n\n
\n\n

Image 2: Pandas Common functions Pandas General Functions

← Pandas Series Str ContainsPandas Series Str Lower β†’