Strip Whitespace in Python

There are various ways to remove spaces from a string in Python. This tutorial is aimed to provide a short example of various functions you can use to remove whitespaces from a string.

Create Text

# Create text with leading and trailing whitespace
text_data = [
    ' Be yourself; everyone else is already taken ',
    ' So many books, so little time.',
    ' You only live once '
]

Remove Whitespace

Use list comprehension with the strip() method to remove leading and trailing whitespace:

# Strip whitespaces
strip_whitespace = [string.strip() for string in text_data]

# Show text
print(strip_whitespace)

Output:

[
    'Be yourself; everyone else is already taken',
    'So many books, so little time.',
    'You only live once'
]

Remove Extra Space Between Text

So far, we have used only functions of the strip-family. However, to get rid of duplicate blank characters between the words of our sentence we need to apply the re.sub operation: https://docs.python.org/2/library/re.html

Import the re module for regular expressions:

import re

# Example string with multiple spaces
my_string = " This   sentence   contains   many   redundant    whitespaces ! "

# Apply sub function to replace multiple spaces with single space
string = re.sub(" +", " ", my_string)

# Strip leading and trailing spaces
string = string.strip()

print(string)
# Output: "This sentence contains many redundant whitespaces !"

Alternative Methods

Using split() and join()

# Remove all extra whitespace by splitting and joining
text = "  This   has   lots   of   spaces  "
cleaned = " ".join(text.split())
print(cleaned)
# Output: "This has lots of spaces"

Using strip() methods

text = "  Hello World  "

# Remove leading and trailing spaces
print(text.strip())        # "Hello World"

# Remove only leading spaces
print(text.lstrip())       # "Hello World  "

# Remove only trailing spaces
print(text.rstrip())       # "  Hello World"

Further Reading

  1. How Python parses whitespace
  2. Splitting and Joining Strings
  3. How to use Split in Python
  4. Remove consecutive spaces in a string