strip() function in Python

The strip() function in Python is a built-in string method used to remove leading (left) and trailing (right) whitespace (spaces, tabs, newlines) or specific characters from a string.

1. Syntax of strip()

str.strip([chars])
  • str: The string on which the method is called.
  • chars (optional): A string containing specific characters to remove from the beginning and end of str. If omitted, it defaults to removing whitespace.

2. How strip() Works

2.1 Removing Whitespace (Default Behavior)

By default, strip() removes any leading and trailing whitespace characters, including:

  • Space (' ')
  • Tab ('\t')
  • Newline ('\n')
  • Carriage return ('\r')

Example 1: Using strip() to Remove Spaces

text = "   Hello, Python!   "
cleaned_text = text.strip()
print(cleaned_text)

Output

Hello, Python!

Note: The spaces before and after "Hello, Python!" are removed.

2.2 Removing Specific Characters

You can pass a string containing characters to remove from both ends of the given string. These characters are treated as a set, not as a substring.

Example 2: Removing Specific Characters

text = "---Python---"
cleaned_text = text.strip("-")
print(cleaned_text)

Output

Python

Note: The "-" characters at both ends are removed, but the ones inside the string remain.

Example 3: Removing Multiple Specific Characters

text = "abcPythonabc"
cleaned_text = text.strip("acb")  # Removes 'a', 'b', and 'c' from both ends
print(cleaned_text)

Output

Python

Explanation: "acb" is treated as a set of characters, so it removes any occurrence of ‘a’, ‘b’, or ‘c’ from both ends.

2.3 Using strip() with Newline and Special Characters

Example 4: Removing Newline (\n), Tab (\t), and Spaces

text = "\n\t Hello, World! \t\n"
cleaned_text = text.strip()
print(cleaned_text)

Output

Hello, World!

Explanation: The strip() method removes the newline (\n), tab (\t), and spaces from both ends.

2.4 Using strip() for File Handling

When reading a file, each line usually contains a trailing newline character (\n). Using strip() helps clean up the text.

Example 5: Removing Newline While Reading a File

# Simulating reading lines from a file
lines = ["Hello, World!\n", " Python is great! \n", "\tData Science\t"]

# Stripping each line
cleaned_lines = [line.strip() for line in lines]

# Printing cleaned lines
for line in cleaned_lines:
    print(line)

Output

Hello, World!
Python is great!
Data Science

Explanation: The strip() method removes unwanted spaces and newline characters from each line.

3. Comparison: strip(), lstrip(), and rstrip()

Python provides two additional string methods:

  • lstrip(): Removes leading (left-side) characters.
  • rstrip(): Removes trailing (right-side) characters.

3.1 Using lstrip() (Removes Leading Characters)

text = "   Hello, World!   "
print(text.lstrip())  

Output

Hello, World!   

Explanation: The spaces on the left are removed, but the right-side spaces remain.

3.2 Using rstrip() (Removes Trailing Characters)

text = "   Hello, World!   "
print(text.rstrip())  

Output

   Hello, World!

Explanation: The spaces on the right are removed, but the left-side spaces remain.

4. Summary Table

MethodRemovesExampleOutput
strip()Leading & trailing characters" Hello ".strip()"Hello"
lstrip()Leading characters only" Hello ".lstrip()"Hello "
rstrip()Trailing characters only" Hello ".rstrip()" Hello"

5. Key Takeaways

  • strip() removes any leading and trailing whitespace or other specified characters.
  • If no argument is passed, it defaults to removing spaces, newlines, and tabs.
  • The chars argument is treated as a set (individual characters) and not as a substring.
  • Use lstrip() and rstrip() if you need to remove characters from only one side.