Remove multiple spaces from a string in Python

In this Python tutorial, we will learn how to remove multiple spaces from a string.

Table Of Contents

Remove multiple spaces from a string using re.sub()

The re is a Regex module in Python. It has a method sub(), and it is used to replace substrings in strings, that matches a given regex pattern. We can use the same to remove multiple spaces from a string. For that we need to use a regex pattern that matches one or more whitespaces. Then look for the substrings that matches the given regex pattern and replace them with single whitespace. Let’s see the syntax of sub() with the parameters that we need to pass in it to remove multiple spaces.

Syntax:

re.sub("\s+", " ", strValue)

Parameters:

  1. the First parameter is a regex pattern that will look for one or more whitespaces.
  2. the second parameter represents a single whitespace
  3. strValue is the actual input string.

It will return a string in which all multiple spaces are replaced by a single space.

Example 1:

In this example, we will consider the string with multiple spaces and remove these multiple spaces using re.sub() method.

import re

# Consider a string with multiple spaces
strValue = "Welcome     to     thisPointer,learn                Python language"

print("Original String: \"", strValue, "\"")

# Remove multiple spaces from string
strValue = re.sub("\s+", " ", strValue)

print("String after removing multiple spaces: \"", strValue, "\"")

Output:

Original String: " Welcome     to     thisPointer,learn                Python language "
String after removing multiple spaces: " Welcome to thisPointer,learn Python language "

You can see that multiple spaces are removed between

  1. welcome and to
  2. to and thisPointer
  3. learn and Python

Example 2:

In this example, we will consider the string with a single space and remove these multiple spaces using re.sub() method.

import re

# Consider a string with multiple spaces
strValue = "Hello           Python"

print("Original String: \"", strValue, "\"")

# Remove multiple spaces from string
strValue = re.sub("\s+", " ", strValue)

print("String after removing multiple spaces: \"", strValue, "\"")

Output:

Original String: " Hello           Python "
String after removing multiple spaces: " Hello Python "

You can see that multiple spaces are removed between Hello and Python.

Remove multiple spaces from a string using join() with split()

we can split a string using the split() function of string class. By default it will use a whitespace character as delimeter to split the string into multiple substrings. Then join all of theese substrings with a single space as separator using the join() method of string class. It will remove the multiple spaces from the string.

Example:

In this example, we have a string with multiple spaces and we will remove these multiple spaces from it using the join() method with split().

strValue = "Welcome     to     thisPointer,learn                Python language"

print("Original String: \"", strValue, "\"")

# Remove multiple spaces from string
strValue = " ".join(strValue.split())

print("String after removing multiple spaces: \"", strValue, "\"")

Output:

Original String: " Welcome     to     thisPointer,learn                Python language "
String after removing multiple spaces: " Welcome to thisPointer,learn Python language "

You can see that multiple spaces are removed between

  1. welcome and to
  2. to and thisPointer
  3. learn and Python

Summary

In this article, we saw how to remove multiple spaces from a string in Python using different techniques. The best method is using split() with join() because no extra module is required to use this method. Thanks.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top